Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

JTabbedPane growing when adding a new tab using a BorderLayout

843806Jun 3 2009 — edited Jun 4 2009
I have a strange behaviour with a basic JTabbedPane: each time I add a new tab, the JtabbedPane is growing even if each component is the same size. I was expecting that the JTabbedPanel will be the size of the biggest component when inside a BorderLayout.
Try this code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Main
{

    public static void main(String args[])
    {
        Runnable runner = new Runnable()
        {

            public void run()
            {
                final JFrame frame = new JFrame("Tabbed Pane Sample");
                JPanel panel = new JPanel(new BorderLayout());
                frame.add(panel);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final JTabbedPane tabbedPane = new JTabbedPane();
                panel.add(tabbedPane, BorderLayout.NORTH);
                JButton add = new JButton("Add");
                add.addActionListener(new ActionListener()
                {

                    public void actionPerformed(ActionEvent e)
                    {
                        tabbedPane.addTab("New tab", new JLabel("Same label"));
                    }
                });
                panel.add(add, BorderLayout.SOUTH);
                JTextArea text = new JTextArea("Alea jacta est, press the buttom 6 times if you can");
                panel.add(text, BorderLayout.CENTER);
                frame.setSize(400, 150);
                frame.setVisible(true);
            }
        };
        EventQueue.invokeLater(runner);
    }
}

Comments

Processing
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 2 2009
Added on Jun 3 2009
5 comments
327 views