Table of contents
1.
Introduction
2.
Grid Layout
2.1.
Constructors in GridLayout class
2.2.
Class Methods
3.
Implementation
3.1.
GridLayout()
3.2.
GridLayout(int rows, int columns)
3.3.
GridLayout(int rows, int columns, int hgap, int vgap)
4.
Frequently Asked Questions
4.1.
What is a grid layout class in Java?
4.2.
How do I create a grid in JFrame?
4.3.
What is a layout in Java?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Grid Layout In Java

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Java AWT, also known as Abstract Window Toolkit, is an API used to create GUI or window-based Java applications. Because Java AWT components are platform-dependent, they are shown in accordance with the operating system's view.

In this blog, we will discuss the grid layout class present in Java. We will look into the different types of constructors present inside the grid layout. We will also discuss how we can use these constructors with their implementation.

Grid Layout

The GridLayout class is a layout manager that organizes the components of a container in a rectangular grid. The GridLayout container is divided into equal-sized rectangles, and each rectangle contains one of the components. Because each rectangle cell is the same size, it contains a component that fills the entire cell. When the user changes or adjusts the container's size, the size of each rectangle changes as well. For example, the applet below divides six buttons into three rows and two columns.

 

Grid Layout

Source: Oracle

Constructors in GridLayout class

The GridLayout class has the following constructors:

Constructors in GridLayout class

Class Methods

The commonly used methods in GridLayout class are:

Class Methods

Implementation

In the below implementation we will have a look at three different programs to create a grid layout which divides the container into a grid of cells called rows and columns and the number of the grid of cells and columns can be adjusted as per the need of the user. In the first program, we will look into the usage of a parameterless constructor while in the last two programs we will be defining the number of rows and columns and also the horizontal and vertical gaps between the cells respectively.

GridLayout()

In this program, we will be implementing an empty constructor with one column per component in a single row.

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

public class GridLayoutDemo {

  public static void main(String[] args) {
    // constructor
    JFrame frame = new JFrame("Grid Layout");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    JPanel panel = new JPanel();

    // creating 4 buttons
    panel.setLayout(new GridLayout());
    JButton button1 = new JButton("1");
    JButton button2 = new JButton("2");
    JButton button3 = new JButton("3");
    JButton button4 = new JButton("4");

    //setting the orientation
    panel.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    frame.add(panel);
  }
}
You can also try this code with Online Java Compiler
Run Code


Output

Output-1

GridLayout(int rows, int columns)

In this program, we will implement Constructor with a specified number of rows and columns.

  • Rows: specifies the number of rows
     
  • Columns: specifies the number of columns
     
import java.awt.*;
import javax.swing.*;

public class GridLayoutDemo {

  public static void main(String[] args) {
    JFrame frame = new JFrame("Grid Layout");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    JPanel panel = new JPanel();

    // setting grid layout of 2 rows and 2 columns
    panel.setLayout(new GridLayout(2, 2));

    // creating 4 buttons
    JButton button1 = new JButton("1");
    JButton button2 = new JButton("2");
    JButton button3 = new JButton("3");
    JButton button4 = new JButton("4");

    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    frame.add(panel);
  }
}
You can also try this code with Online Java Compiler
Run Code


Output

Output-2

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

In this program, we will insert horizontal and vertical gaps between buttons using the parameterized constructor GridLayout(int rows, int columns, int hgap, int vgap).

  • Rows: specifies the number of rows
     
  • Columns: specifies the number of columns
     
  • hgap: the gap between each of the columns
     
  • vgap: the gap between each of the rows
     
import java.awt.*;
import javax.swing.*;

public class GridLayoutDemo {

  public static void main(String[] args) {
    // constructor
    JFrame frame = new JFrame("Grid Layout");
    frame.setVisible(true);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 200);
    JPanel panel = new JPanel();

    // setting the grid layout  
    // a 2 * 2 grid is created with the horizontal gap 5
    // and vertical gap 10
    panel.setLayout(new GridLayout(2, 2, 5, 10));

    // creating 4 buttons
    JButton button1 = new JButton("1");
    JButton button2 = new JButton("2");
    JButton button3 = new JButton("3");
    JButton button4 = new JButton("4");

    //setting the orientation
    panel.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
    panel.add(button1);
    panel.add(button2);
    panel.add(button3);
    panel.add(button4);
    frame.add(panel);
  }
}
You can also try this code with Online Java Compiler
Run Code

 

Output

Output-3

Frequently Asked Questions

What is a grid layout class in Java?

It is a layout manager that organizes the components of a container in a rectangular grid.
 

How do I create a grid in JFrame?

To make a new frame, use the syntax: JFrame frame = new JFrame(); We must ensure that all of the buttons are placed in the grid layout within the constructor method.
 

What is a layout in Java?

The arrangement of components within the container is referred to as layout. In other words, the components are placed in a certain location within the container. The Layout Manager handles the task of layouting the controls automatically.

Conclusion

In this article, we have extensively discussed the grid layout present inside Java. We have seen different constructors to initialize the grid layout. Also, we have seen various methods available inside the class along with the implementation.

If you think this blog has helped you enhance your knowledge about grid layout and if you would like to learn more, check out our articles Understanding JavaGrid LayoutIntroduction to PackagesJDK, and many more in our Library.

Refer to our Guided Path on Coding Ninjas Studio to upskill yourself in Data Structures and AlgorithmsCompetitive ProgrammingJavaScriptSystem Design, and many more! If you want to test your competency in coding, you may check out the mock test series and participate in the contests hosted on Coding Ninjas Studio! But suppose you have just started your learning process and are looking for questions asked by tech giants like Amazon, Microsoft, Uber, etc. In that case, you must look at the problemsinterview experiences, and interview bundle for placement preparations.

Do upvote our blogs if you find them helpful and engaging!

Happy Learning!

Live masterclass