Search code examples
javaswinglayout-managergridbaglayout

How to get middle column bigger than side columns in GridBagLayout?


I am trying to do a 3 column layout. How can I get the middle column bigger then side column? I am using GridBagLayout from Java Swing. I was messing around with the GridWidth but it was not working.

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class GridBag extends JFrame{

    JPanel j1, j2, j3;
    GridBagConstraints gbc = new GridBagConstraints();
    public GridBag() {
        super("Pandemic");
        setSize(1000, 500);         //suggested size
        setLocationRelativeTo(null);    
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLayout(new GridBagLayout());
        j1 = new JPanel();
        j1.setBackground(Color.blue);
        j2 = new JPanel();
        j2.setBackground(Color.red);
        j3 = new JPanel();
        j3.setBackground(Color.yellow);
        gbc.weightx = 0.5;
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.ipady = 1000;
        this.add(j1, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        this.add(j2, gbc);
        
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridy = 0;
        this.add(j3, gbc);
        
        this.setVisible(true);
        
    }
    public static void main(String[] args) {
        new GridBag();

    }
    //end main
}

https://i.sstatic.net/rJwQr.png


Solution

  • I was messing around with the gridwidth but it was not working.

    You can't just randomly give a component a gridwidth of a random number. You only have 3 components on a single row, so each component can only have a width of 1.

    However, if you had a second row with two components, then you could give one of the components a width of 2 and the other a width of 1.

    How can I get the middle column bigger then side columns ?

    gbc.weightx = 0.5;
    

    Above specifies how extra space is allocated to each component when the size of the frame is greater than the preferred size of each component.

    If you don't change the constraint, then the same value is used for all components (since you use the same GridBagConstraints object).

    In your example all components use the same "weightx" and therefore all the components will be the same size.

    This is the constraint you need to change. It needs to be different for your components if you want them to have different sizes.

    Try using:

    gbc.weightx = 0.25;
    

    for the blue and yellow components. Now the red component will be roughly twice the size.

    gbc.ipady = 1000;  
    

    You should probably be using the "fill" constraint, to fill the vertical height of each panel. Then it will automatically grow to fill the space available in the frame.

    Read the Swing tutorial on How to Use GridBagLayout for more information and examples.