3

I have a simple problem when I want to add tabs in my jpanel. The alignment of the tabs get horizontal instead of vertical, wich looks like crap =/.

It looks like this:

enter image description here

If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine. If you uncomment the three lines of code and remove the getContentPane().add(jtp); you can reproduce my probleme.

working Code:

public class TabbedPane extends JFrame
{
    public TabbedPane()
    {
        setTitle("Tabbed Pane");
        setSize(300, 300); // set size so the user can "see" it
        JTabbedPane jtp = new JTabbedPane();

        // JPanel panel = new JPanel();//uncomment all three lines
        // panel.add(jtp);
        // getContentPane().add(panel);

        getContentPane().add(jtp);//remove me

        JPanel jp1 = new JPanel();// This will create the first tab
        JPanel jp2 = new JPanel();// This will create the second tab

        JLabel label1 = new JLabel();
        label1.setText("This is Tab 1");
        jp1.add(label1);

        jtp.addTab("Tab1", jp1);
        jtp.addTab("Tab2", jp2);

        JButton test = new JButton("Press");
        jp2.add(test);

        setVisible(true); // otherwise you won't "see" it
    }

    public static void main(String[] args)
    {
        TabbedPane tab = new TabbedPane();
    }

}

Thanks a lot!

Thkru
  • 4,218
  • 2
  • 18
  • 37
  • I think you just need to set the size: http://stackoverflow.com/questions/2510310/how-to-make-jtabbedpane-autoresize-to-fit-page-dimensions – James Jan 27 '13 at 22:27

5 Answers5

4

If I discard the panel instead and add the tabbedPane directly to the frame, everything works fine.

The default layout of JPanel is FlowLayout, which "lets each component assume its natural (preferred) size." The default layout of JFrame is BorderLayout, the CENTER of which ignores preferred size. In either case, invoking setSize() precludes the layout from functioning initially; re-size the frame to see the effect. Instead, use pack(), which "Causes this Window to be sized to fit the preferred size and layouts of its subcomponents."

setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true); // otherwise you won't "see" it
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • As far as I can follow you, you are right. But anyhow I dont see the relation how this solves my problem. +1 anyway ;) – Thkru Jan 28 '13 at 01:47
  • @ThomasK: Layout managers are designed to accommodate components of varying size in response to resizing the enclosing window; `pack()` does the initial adjustment. You may have to experiment a little to see the effect; more [here](http://stackoverflow.com/q/7229226/230513) and [here](http://stackoverflow.com/a/14555985/230513). – trashgod Jan 28 '13 at 09:58
3

There are many things I would change in that code, starting with the recommendations of @trashgod. OTOH this is the minimal change needed in order to stretch the tabbed pane to the width/height of the parent container.

// give the panel a layout that will stretch components to available space
JPanel panel = new JPanel(new GridLayout());//uncomment all three lines
panel.add(jtp);
getContentPane().add(panel);

//getContentPane().add(jtp);//remove me

For more details see this answer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

Well firstly you can try this:

    JPanel panel = new JPanel();//uncomment all three lines
    panel.setLayout(new GridLayout());
    JPanel jp1 = new JPanel();// This will create the first tab
    JPanel jp2 = new JPanel();// This will create the second tab

    JLabel label1 = new JLabel();
    label1.setText("This is Tab 1");
    jp1.add(label1);

    jtp.addTab("Tab1", jp1);
    jtp.addTab("Tab2", jp2);

    JButton test = new JButton("Press");
    jp2.add(test);
    getContentPane().add(jtp);

and in the main:

    TabbedPane tab = new TabbedPane();
    tab.pack();
    tab.setVisible(true);

May I suggest using MigLayout to set layouts, it will make your life easier. Hope it helps.

Ivo
  • 450
  • 3
  • 18
1

Try GridbagLayout. Once you have mastered it, you can design UI of any sort with this layout.

prasanth
  • 3,502
  • 4
  • 28
  • 42
  • *"Once you have mastered it,.."* Are you a master of GBL? *"you can design UI of any sort"* Really? Here's a challenge for you, design a GUI with the same layout as shown in the [Nested Layout Example](http://stackoverflow.com/a/6755801/418556) using ***only*** GBL. Don't worry about the titled borders, but replicate the effect of the rest of the GUI. – Andrew Thompson Jan 29 '13 at 00:51
0

I agree with prasanth regarding the use of GridBagLayout

I have gone through this problem once and I solved it by adding the JTabbedPaneto the panel via GridBagLayout, make sure you add the JTabbedPane using the ipadx and ipady according to your requirements in your GridBagConstraints object e.g.

JPanel myPanel=new JPanel();
myPanel.setLayout(new GridBagLayout());
JTabbedPane jTP=new JTabbedPane();
jTP.add("Tab1",new JPanel());//substitute your component instead of "new JPanel"
GridBagConstraints myConstraints=new GridBagConstraints();
myConstraints.ipadx=400;//streches the component being added along x axis - 200 px on both sides
myConstraints.ipady=600;//streches the component being added along y axis - 200 px on both sides
myPanel.add(jTP,myConstraints);

You can adjust both these properties according to what is perfect for your need

Community
  • 1
  • 1