Java Documentation

GridLayout: Equal-Sized Cells

GridLayout divides the container into equal-sized rows and columns, placing one component in each cell.

Example: 3x3 Grid of Buttons

This example creates a 3x3 grid with 10-pixel gaps between cells and adds nine buttons.

GridLayoutDemo.java
import javax.swing.*;
import java.awt.*;

class MyFrame extends JFrame {
    MyFrame() {
        setTitle("Layouts - GridLayout");

        setLayout(new GridLayout(3, 3, 10, 10));

        /*
        Here GridLayout can be called as: 
            1) GridLayout(): General Grid;
            2)GridLayout(int row, int col): frame into row and columns
                    
                GrifLayouut(2, 2): 2 rows and 2 columns;


            3) GridLayout(int row, int col, int hgap, int vgap): 
                giving specific horizontal gap and Vertical gap between items of row and column
        */
       JButton b1 = new JButton("Button 01");
       JButton b2 = new JButton("Button 02");
       JButton b3 = new JButton("Button 03");
       JButton b4 = new JButton("Button 04");
       JButton b5 = new JButton("Button 05");
       JButton b6 = new JButton("Button 06");
       JButton b7 = new JButton("Button 07");
       JButton b8 = new JButton("Button 08");
       JButton b9 = new JButton("Button 09");

       add(b1);
       add(b2);
       add(b3);
       add(b4);
       add(b5);
       add(b6);
       add(b7);
       add(b8);
       add(b9);

        setVisible(true);
        setSize(500, 500);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

public class GridLayoutDemo {
    public static void main(String arr[]) {
        new MyFrame();
    }
}
Reference & full codeView on GitHub

What is GridLayout?

GridLayout arranges components in a table-like grid. All cells have the same size.

  • Specify rows and columns, optionally gaps between cells.
  • Good for keypads, button panels, or uniform forms.
  • Order of add() decides where each component goes.

Line-by-line: Understanding the GridLayout code

Here we look at how the grid is defined and filled.

setLayout(new GridLayout(3, 3, 10, 10));

Creates a 3x3 grid with horizontal and vertical gaps of 10 pixels.

JButton b1 = new JButton("Button 01"); ...
add(b1); add(b2); ... add(b9);

Creates nine buttons and adds them in order. GridLayout fills cells row by row.

When to use GridLayout

Use GridLayout when all components should have the same size and align in rows and columns, such as calculators or option grids.