GridLayout
GridLayout as Layout Manager in Java
Outline: GridLayout
GridLayout is a layout manager in Java that lets you create user interfaces with a grid-like structure. GridLayout is useful for creating apps with a consistent look and feel. It also makes it easier to arrange content into groups based on type, such as headings, lists or images. You can use grid layout to speed up your app by arranging your content into logical groups.
GridLayout provides a default grid pattern in its layouts. A grid layout consists of rows and columns where every row has the same number of columns and every column has the same number of rows.
Guide: GridLayout
In this article, we will explore GridLayout as Layout Manager in Java. In other to achieve this, the following steps can be adhered to:
1 – Create a Java program with file name, gridLayout.java in JCreator or NetBeans.
2 – Proceed to import the corresponding packages:
import java.awt.*; //used to access of GridLayout class
import javax.swing.*; //used to access the JFrame and JButton class
3 – Now, initiate the variable in your main;
– variable frame for JFrame
– Insert 6-buttons; b1,b2,b3,b4,b5, and b6 as JButton.
JFrame frame = new JFrame("GridLayout as Layout Manager");
JButton b1, b2, b3, b4, b5, b6;
b1 = new JButton("Button1");
b2 = new JButton("Button2");
b3 = new JButton("Button3");
b4 = new JButton("Button4");
b5 = new JButton("Button5");
b6 = new JButton("Button6");
4 – Using the setLayout method of the frame to have a GridLayout as the Layout manager.
frame.getContentPane().setLayout(new GridLayout(2,3));
Take careful notice to see the 2 parameters of GridLayout class.
– 2 indicates the number of rows
– 3 indicates the column of the grid
We indicate the number o rows and columns when creating a GridLayout object and the container will be divided properly into the grid.
Proceed to use the add method to add the buttons into the frame.
frame.getContentPane().add(b1);
frame.getContentPane().add(b2);
frame.getContentPane().add(b3);
frame.getContentPane().add(b4);
frame.getContentPane().add(b5);
frame.getContentPane().add(b6);
5 – Finally, set the visibility, size, and the close operation of the frame by using the code below;
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Output: GridLayout

Complete Code: GridLayout as Layout Manager in Java
import java.awt.*; //used to access of GridLayout class
import javax.swing.*; //used to access the JFrame and JButton class
public class gridLayout{
public static void main(String[] args) {
JFrame frame = new JFrame("GridLayout as Layout Manager");
JButton b1, b2, b3, b4, b5, b6;
b1 = new JButton("Button1");
b2 = new JButton("Button2");
b3 = new JButton("Button3");
b4 = new JButton("Button4");
b5 = new JButton("Button5");
b6 = new JButton("Button6");
frame.getContentPane().setLayout(new GridLayout(2,3));
frame.getContentPane().add(b1);
frame.getContentPane().add(b2);
frame.getContentPane().add(b3);
frame.getContentPane().add(b4);
frame.getContentPane().add(b5);
frame.getContentPane().add(b6);
frame.setSize(500, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}