Search code examples
javaswinglayout-managergridbaglayout

GridBagLayout - How can I make this cell fill the space below it?


I'm creating a JFrame with 4 JPanels. Using GridBagLayout, I create three rows and two columns, two panels per column. By changing the blue panel's cellheight from 1 to 2 I can make it cover the cell below it. I'd like to do the same for the green panel, to fill the space below it. Here's what my code currently produces:

enter image description here

I tried changing the green panel's gridheight to 2 but I end up with this:

enter image description here

Am I using GridBagLayout incorrectly? What is the appropriate way of doing this?

Here's my code:

import java.awt.Color;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        addComponents(frame.getContentPane());
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public static void addComponents(Container contentPane) {
        contentPane.setLayout(new GridBagLayout());
        GridBagConstraints c = new GridBagConstraints();
        c.insets = new Insets(3,3,3,3);
        c.fill = GridBagConstraints.BOTH;

        c.gridx = 0;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 2;
        c.weightx = 1;
        c.weighty = 0.85;
        JPanel panel1 = new JPanel();
        panel1.setBackground(Color.BLUE);
        contentPane.add(panel1, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 1.1;
        c.weighty = 0.35;
        JPanel panel2 = new JPanel();
        panel2.setBackground(Color.YELLOW);
        contentPane.add(panel2, c);

        c.gridx = 1;
        c.gridy = 0;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.15;
        c.weighty = 0.5;
        JPanel panel3 = new JPanel();
        panel3.setBackground(Color.RED);
        contentPane.add(panel3, c);

        c.gridx = 1;
        c.gridy = 1;
        c.gridwidth = 1;
        c.gridheight = 1;
        c.weightx = 0.38;
        c.weighty = 0.5;
        JPanel panel4 = new JPanel();
        panel4.setBackground(Color.GREEN);
        contentPane.add(panel4, c);
    }
}

Solution

  • I create three rows and two columns,

    Actually you only have two rows, since you only ever add components with a gridy value of 0 and 1. The fact that one of the components has a gridheight of 2 doesn't create a new row.

    One solution is to use nested panels.

    Create a panel for the blue and yellow components. This would use a GridBagLayout with one column and 2 rows. You then set the weighty values for each component to give your desired height.

    Then you create a second panel for the red and green components again with 1 column and two rows. The weighty would be set to 0.5 for each.

    Finally you create a third panel with 2 columns and one row. You set the desired weightx and add the above two panels to this panel and then add the panel to the frame.