GridBagLayout : gridwidth, gridheight and fill

The gridwidth variable lets you specify the number of columns in the display area of the component. The default value is 1 Pixel. To specify that a component use the remaining space in a row, use GridBagConstraints.REMAINDER. To specify that a component use the next-to-last cell in a row, use GridBagConstraints.RELATIVE. The gridheight constraint works the same way, but in the vertical direction.

fill : To resize a component when it is smaller than the display area, the valid values of the this data member are provided in below .

GridBagConstraints.NONE ;                               // Default option.
GridBagConstraints.BOTH;                                // To fill the entire display area.
GridBagConstraints.HORIZONTAL;               // To fill the diplay area horizontally. Height will not be changed.
GridBagConstraints.VERTICAL;                       // To fill the display area vertically. Width will not be changed.

Program

import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;

class GridBagLayt extends JFrame {
 
    GridBagLayt()
    {
        setTitle("GridBagLayout");
        GridBagLayout lay = new GridBagLayout();
        GridBagConstraints cons = new GridBagConstraints();
        setLayout(lay);
     
        cons.fill = GridBagConstraints.BOTH;
        setAvailableGridheight(cons);
        setButton("button01",0,0,cons,lay);
        setDefaultGridheight(cons);
     
        setFullGridwidth(cons);
        setButton("button02",0,1,cons,lay);
        setDefaultGridwidth(cons);

        setButton("button03",1,1,cons,lay);
     
        setFullGridheight(cons);
        setButton("button04",1,2,cons,lay);
        setDefaultGridheight(cons);
     
        setButton("button05",2,1,cons,lay);
     
        setAvailableGridwidth(cons);
        setButton("button06",3,0,cons,lay);
        setDefaultGridwidth(cons);
     
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(700,200);
        setVisible(true);
    }
 
    void setButton(String name,int y,int x,GridBagConstraints gbc,GridBagLayout lay)
    {
        JButton jb = new JButton(name);
        gbc.gridy = y;
        gbc.gridx = x;
        lay.setConstraints(jb, gbc);
        add(jb);
    }
 
    void setAvailableGridwidth(GridBagConstraints gbc)
    {
        gbc.gridwidth = GridBagConstraints.RELATIVE;
    }
 
    void setFullGridwidth(GridBagConstraints gbc)
    {
        gbc.gridwidth = GridBagConstraints.REMAINDER;
    }
 
    void setAvailableGridheight(GridBagConstraints gbc)
    {
        gbc.gridheight = GridBagConstraints.RELATIVE;
    }
 
    void setFullGridheight(GridBagConstraints gbc)
    {
        gbc.gridheight = GridBagConstraints.REMAINDER;
    }
 
    void setDefaultGridwidth(GridBagConstraints gbc)
    {
        gbc.gridwidth = 1;
    }
 
    void setDefaultGridheight(GridBagConstraints gbc)
    {
        gbc.gridheight = 1;
    }
}

public class Javaapp {
 
    public static void main(String[] args) {
     
        GridBagLayt gb = new GridBagLayt();
    }
}

Program Output

Leave a Comment