JTabbedPane in JPanel?

I have a simple problem where I want to add tabs to my jpanel. The alignment of the tabs becomes horizontal instead of vertical, which looks like crap = /.

It looks like this:

enter image description here

If I drop the pane and add the tabbedPane directly to the frame, everything is fine. If you uncomment the three lines of code and delete getContentPane().add(jtp);

, you can reproduce my problem.

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();
    }

}

      

Thank you so much!

+3


source to share


5 answers


If I drop the panel and add tabbedPane

directly to the frame, everything is fine.

A standard layout JPanel

is one FlowLayout

that "allows each component to assume its natural (preferred) size". The default layout JFrame

is BorderLayout

, CENTER

which is ignored by the preferred size. In any case, the call setSize()

excludes the possibility of linking initially; resize the frame to see the effect. Instead, use pack()

which "calls the size Window

to fit the preferred sizes and layouts of its subcomponents."



setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setVisible(true); // otherwise you won't "see" it

      

+4


source


There are tons of things in this code, starting with @trashgod's advice. OTOH is the minimum change required to stretch the tabbed panel 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

      



See this answer for details .

QsSEU.png

+3


source


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 basically:

    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.

+1


source


Try GridbagLayout . Once you have mastered it, you can design any type of user interface with this layout.

+1


source


I agree with prasanth regarding the use ofGridBagLayout

I looked at this problem once and I solved it by adding JTabbedPane

to the panel via GridBagLayout

, make sure you add JTabbedPane

with ipadx

and ipady

as per your requirements to your object GridBagConstraints

eg.

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 customize both of these properties to suit your needs.

0


source







All Articles