Home » Java GridLayout

Java GridLayout

The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle.

Constructors of GridLayout class

  1. GridLayout(): creates a grid layout with one column per component in a row.
  2. GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components.
  3. GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.

Example of GridLayout class: Using GridLayout() Constructor

The GridLayout() constructor creates only one row. The following example shows the usage of the parameterless constructor.

FileName: GridLayoutExample.java

Output:

Java GridLayout

Example of GridLayout class: Using GridLayout(int rows, int columns) Constructor

FileName: MyGridLayout.java

Output:

Java GridLayout

Example of GridLayout class: Using GridLayout(int rows, int columns, int hgap, int vgap) Constructor

The following example inserts horizontal and vertical gaps between buttons using the parameterized constructor GridLayout(int rows, int columns, int hgap, int vgap).

FileName: GridLayoutExample1.java

Output:

Java GridLayout


Next TopicFlowLayout

You may also like