header

GridBagLayout & GridBagConstraints Class

GridBagLayout Class

Layout classes (also referred to as layout managers) are used to determine how components are laid out on a window. The gridbag layout manager divides the window into a rectangular grid of cells. Each cell is corresponds to its (x, y) coordinates where x specifies the column (starting at 0) and y specifies the row (starting at 0). Components can be placed on this grid in any order and any cell can be left empty if desired.

Unlike the grid layout manager, the gridbag layout manager does not automatically set every cell to exactly the same size. The width of any given column is determined by the width of the widest component in that column. Similarly, the height of any given row is determined by the height of the tallest component in that row.

Another way in which the gridbag layout differs from the grid layout, is that a single component can span two or more columns and/or two or more rows. With a grid layout, if the window is made larger, the individual components are drawn at a larger size since the cell size is determined by the window size. When using a grid bag layout, this doesn't happen because the cell sizes are determined by the sizes of the components not the size of the window. Consequently, if the user makes the window larger, the grid bag layout will look the same except for a larger margin on all four sides.

Import

import java.awt.GridBagLayout;

Constructor

GridBagLayout()

The only constructor is the default constructor.

GridBagConstraints Class

Adding components to a gridbag layout requires an instance of the GridBagConstraints Class which is used to specify where a component is placed and how it is to be displayed. These settings are determined by assigning values to the instance variables of a GridBagConstraints object. When using a gridbag layout, the add method requires two parameters; the component and the GridBagConstraints object.

The gridx and gridy Variables

The value of the gridx instance variable determines the column in which the object will be placed and the value of the gridy instance variable determines the row.

The gridwidth and gridheight Variables

The value of the gridwidth instance variable specifies how many columns the component is to span. The default value is 1. If this value is more than one, then the component will span the specified number of columns starting with the column specified by the value of the gridx variable and counting to the right.

The value of the gridheight instance variable specifies how many rows the component is to span. The default value is 1. If this value is more than one, then the component will span the specified number of rows starting with the row specified by the value of the gridy variable and counting downward.

The insets Variable

The insets variable is an instance of the Insets class. An Insets object specifies the distance between a component and the edge of the cell containing the component. The inset from the top, left, bottom, and right are set individually by assigning the insets variable a new instance of the Insets class. The default insets are 0 pixels for all four sides.

The fill Variable

The value of the fill variable determines how components are to be resized if the component is smaller than the cell in which the component is placed. There are four possible values:

Import

import java.awt.GridBagConstraints;

Constructor

GridBagConstraints()

Example 1

In this example, the graphical user interface uses a GridBagLayout with three rows and two columns. Four components are added to the frame. The graphic below indicates the size and location of the grid cells (the lines have been added and would not actually appear on the interface). Notice, in particular, that the columns are of different width; each width determined by the widest component in the column.

Here is the code needed to create this interface:

public class GridBagLayoutEx implements ActionListener
{
    private JFrame myFrame;
    private JTextField txtInput;
    
    // Main method is not shown.
    
    /**
     * Create and show the graphical user interface.
     */
    private void createAndShowGUI()
    {
        txtInput = new JTextField(5);
        
        GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.HORIZONTAL;
        
        myFrame = new JFrame("GridBagLayout Example");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLayout(new GridBagLayout());
        
        gc.gridx = 0;
        gc.gridy = 0;
        myFrame.add(new JLabel("GridLayout Demo", SwingConstants.CENTER), gc);
        
        gc.gridx = 0;
        gc.gridy = 1;
        myFrame.add(new JLabel("Prompt", SwingConstants.RIGHT), gc);
        
        gc.gridx = 1;
        gc.gridy = 1;
        myFrame.add(txtInput, gc);
        
        gc.gridx = 0;
        gc.gridy = 2;
        myFrame.add(new JLabel("Copyright Message", SwingConstants.CENTER), gc);
        myFrame.pack();
        myFrame.setMinimumSize(myFrame.getPreferredSize());
        myFrame.setVisible(true);
    }

    // ActionPerformed method is not shown.
}

Example 2

This second example illustrates the ability to specify that a component can span multiple cells; in this case, multiple columns. The labels in the first and third rows span both columns. Also, the insets variable of the GridBagConstraints object was used to provide a little spacing between each component and the edge of the cell that contains it.

Here is the code needed to create this interface (additions are indicated by bold blue font).

public class GridBagLayoutEx implements ActionListener
{
    private JFrame myFrame;
    private JTextField txtInput;
    
    // Main method is not shown.
    
    /**
     * Create and show the graphical user interface.
     */
    private void createAndShowGUI()
    {
        Container myFrame;

        txtInput = new JTextField(5);
        
        GridBagConstraints gc = new GridBagConstraints();
        gc.fill = GridBagConstraints.HORIZONTAL;
        gc.insets = new Insets(3, 3, 3, 3);
        
        myFrame = new JFrame("GridBagLayout Example");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        myFrame = myFrame.getContentPane();
        myFrame.setLayout(new GridBagLayout());
        
        gc.gridx = 0;
        gc.gridy = 0;
        gc.gridwidth = 2;
        myFrame.add(new JLabel("GridLayout Demo", SwingConstants.CENTER, gc);
        gc.gridwidth = 1;
        
        gc.gridx = 0;
        gc.gridy = 1;
        myFrame.add(new JLabel("Prompt", SwingConstants.RIGHT), gc);
        
        gc.gridx = 1;
        gc.gridy = 1;
        myFrame.add(txtInput, gc);
        
        gc.gridx = 0;
        gc.gridy = 2;
        gc.gridwidth = 2;
        myFrame.add(new JLabel("Copyright Message", SwingConstants.CENTER), gc);
        myFrame.pack();
        myFrame.setMinimumSize(myFrame.getPreferredSize());
        myFrame.setVisible(true);
    }

    // ActionPerformed method is not shown.
}