noobProgrammer
noobProgrammer

Reputation: 477

Layout settings for JTabbedPane

I have a JTabbedPane in a java program. I added a JPanel to each "tab" but when I style the elements inside each panel (GridBagLayout and BorderLayout), there seems to be no effect. Am I doing something wrong? Is there a certain way I need to control the layout? Here's one portion of the code:

public static void main(String args[]) {
    // set L&F
    try {
        for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
            if ("Nimbus".equals(info.getName())) {
                UIManager.setLookAndFeel(info.getClassName());
                break;
            }
        }
    } catch (Exception e) {

    }

    JFrame main = new JFrame();
    JTabbedPane tabs = new JTabbedPane();
    JPanel checkOutPanel = new JPanel();

    Font f = new Font("Header", Font.BOLD, 24);
    Font f2 = new Font("Menu", Font.BOLD, 36);

    Font font = new Font("Menu", Font.BOLD, 24);

    // check out
    JLabel checkOutLabel = new JLabel("Checkout");
    JLabel bookNumLabel = new JLabel("Book Number");
    JLabel personNameLabel = new JLabel("Person Name");

    final JTextField bookNumEntry = new JTextField(20);
    final JTextField personNameEntry = new JTextField(20);
    JButton checkOutButton = new JButton("Check Out");
    checkOutLabel.setFont(font);
    // checkout gui
    GridBagConstraints co = new GridBagConstraints();
    co.gridx = 1;
    checkOutPanel.add(checkOutLabel, co);
    co.gridx = 0;
    co.gridy = 1;
    checkOutPanel.add(bookNumLabel, co);
    co.gridx = 1;
    checkOutPanel.add(bookNumEntry, co);
    co.gridx = 0;
    co.gridy = 2;
    checkOutPanel.add(personNameLabel, co);
    co.gridx = 1;
    checkOutPanel.add(personNameEntry, co);
    co.gridx = 2;
    checkOutPanel.add(checkOutButton, co);

    tabs.addTab("Checkout",checkOutPanel );
    tabs.setTabPlacement(JTabbedPane.LEFT);

    main.add(tabs);
    main.setSize(1300,1100);
    main.setVisible(true);
}

Upvotes: 1

Views: 1701

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347194

You've not set the layout manager for checkOutPanel so it's using it's default layout manager of FlowLayout

Change...

JPanel checkOutPanel = new JPanel();

To something more like...

JPanel checkOutPanel = new JPanel(new GridBagLayout());

Upvotes: 3

Related Questions