Java Swing GridLayout

GridLayout places components in a cell of a grid. Each cell has the same size therefore each component takes up the same space in a container. When user adjusts the container, the size of each component changes accordingly.

Here are the constructors of the GridLayout class:

ConstructorsDescription
public GridLayout(int rows, int cols)Creates a grid layout with a given number of rows and columns. If cols or rows is zero, any numbers of components can be placed in a column or in a row.
public GridLayout(int rows, int cols, int hgap, int vgap)Creates a grid layout with a given number of rows and columns. Beside this, you can initialize the vertical and horizontal gap between each of rows and columns.

Here is the screenshot of GridLayout demo application.

Java GridLayout