Search code examples
javaswinggridbaglayout

Java Swing GridBagLayout - Issue with placing two elements on a panel


First i'll show you how it is:

picture_how_it_is

This is how it should be (see link below in my comment):

Label has to be up and TabPane has to fill the rest of the screen with margins in all directions.

This is the code for laying it out with GridBagLayout:

// Layout --begin--
    this.mainPanel.setLayout(new GridBagLayout());
    GridBagConstraints gbc = new GridBagConstraints();

    // Layout:headLineLabel --begin--
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.HORIZONTAL;
    gbc.ipadx = 0;
    gbc.ipady = 0;
    gbc.insets = new Insets(0, 10, 0, 0);
    gbc.anchor = GridBagConstraints.FIRST_LINE_START;
    this.mainPanel.add(this.headLineLabel, gbc);
    // Layout:headLineLabel --end--

    // Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --begin--
    gbc.gridx = 0;
    gbc.gridy = 1;
    gbc.gridwidth = 1;
    gbc.gridheight = 1;
    gbc.fill = GridBagConstraints.BOTH;
    gbc.ipadx = 0;
    gbc.ipady = 0;
    gbc.insets = new Insets(10, 10, 10, 10);
    gbc.anchor = GridBagConstraints.CENTER;
    this.mainPanel.add(new FestplattenreinigerGraphicalUserInterfaceTabbedPane(), gbc);
    // Layout:FestplattenreinigerGraphicalUserInterfaceTabbedPane --end--
    // Layout --end--

Things you need to know:

  1. FestplattenreinigerGraphicalUserInterfaceTabbedPane extends JTabbedPane.
  2. I tried to specify all constraints (even with their default-val) to practice.
  3. If I commend out the achors, layout remains how it is.

What's wrong in this thing?

THX very much! (sorry i couldnt post images directly + only 1 link due to i'm new -.-)


Solution

  • You have left out weightx and weighty which are necessary or everything will just lump together in the middle. So before adding the tabpane add:

    gbc.weightx = 1.0;
    gbc.weighty = 1.0;