header

GridLayout Class

Layout classes (also referred to as layout managers) are used to determine how components are laid out on a window. The grid layout manager divides the window into a rectangular grid of equal-sized regions. Components are placed on this grid from left to right and top to bottom

You cannot skip any cells as you add components. However, you can add a blank label to a cell you don't really want to use.

If the user resizes the window, the new window area is divided into the same grid arrangement but with the cell size adjusted to fit the new window dimensions. A layout that looks good at one window size may not look nearly as nice if the window is resized.

Import

import java.awt.GridLayout;

Constructors

GridLayout()
GridLayout(int rows, int cols)
GridLayout(int rows, int cols, int hgap, int vgap)

The default constructor creates a grid with one row and as many columns as needed to display all of the components. In the other constructors, you can use the rows parameter to set the number of rows or you can use the cols parameter to set the number of columns, but you can not do both at the same time.

To specify the number of rows, use a positive value for the rows parameter and zero for the cols parameter; for example, new GridLayout(3, 0). Java will create as many columns as needed to display all of the components. The number of columns is the number of components divided by the number of rows rounded up to an integer. In the example shown above, the number of rows is set at three and the grid layout contains five components. Five divided by three is one and two-thirds which rounds up to two columns.

To specify the number of columns, the rows parameter should be zero and the cols parameter should be a positive value; for example, new GridLayout(0, 2). Java will create as many rows as needed to display all of the components. The number of rows is the number of components divided by the number of columns rounded up to an integer. In the example shown above, the number of columns is set at two and the grid layout contains five components. Five divided by two is two and a half which rounds up to three rows.

The layout will have the specified number of rows (or columns) even if there are a fewer number of components. In that case, there will be blank cells in the remaining rows (or columns).

The GridLayout illustrated above could have been constructed in two different ways. One way would be to specify the number of rows: GridLayout(3,0). The other way would be to specify the number of columns: GridLayout(0,2). It is not a syntax error to use positive values for both the rows and cols parameters. For example, you could also use GridLayout(3,2) for the layout shown above. However, if the rows parameter is a positive number, Java just ignores the cols parameter.

The hgap and vgap parameters determine the horizontal gap and vertical gap between adjacent grid cells. The default in both cases is zero pixels.

Example

In this example, the graphical user interface uses a GridLayout with two columns. Five components are added to the frame which results in a layout with three rows (and two columns). Since I really didn't want anything in the upper-right hand cell (the cell in the second column of the first row), I added a blank label to fill that cell. It will not show up when the user interface is displayed. 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).

Here is the code needed to create this interface:

public class GridLayoutExample implements ActionListener
{
    private JFrame myFrame;
    private JLabel lblTitle;
    private JLabel lblPrompt;
    private JLabel lblCopyright;
    private JTextField txtInput;
    
    // Main method is not shown.
    
    /**
     * Create and show the graphical user interface.
     */
    private void createAndShowGUI()
    {
        lblTitle = new JLabel("GridLayout Demo", SwingConstants.CENTER);
        lblPrompt = new JLabel("Prompt", SwingConstants.RIGHT);
        txtInput = new JTextField(5);
        lblCopyright = new JLabel("Copyright Message", SwingConstants.CENTER);
        
        myFrame = new JFrame("GridLayout Example");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLayout(new GridLayout(0, 2));
        myFrame.add(lblTitle);
        myFrame.add(new JLabel());  // Just takes up space
        myFrame.add(lblPrompt);
        myFrame.add(txtInput);
        myFrame.add(lblCopyright);
        myFrame.pack();
        myFrame.setVisible(true);
    }

    // ActionPerformed method is not shown.
}