Differences Between GridLayout and GridBagLayout in Java



In Java, a GridLayout puts all the components in a rectangular grid and is divided into equal-sized rectangles, and each component is placed inside a rectangle whereas GridBagLayout is a flexible layout manager that aligns the components vertically and horizontally without requiring that the components be of the same size.

Java GridLayout

A GridLayout arranges the components in a rectangular grid. It arranges components in the cells, and each cell has the same size. Components are placed in columns and rows. GridLayout(int rows, int columns) takes two parameters that are a column and a row.

Syntax

The following is the syntax for GridLayout initialization:

JPanel panel = new JPanel(new GridLayout(2, 3));
panel.add(new JButton("1"));

When the container is resized, all cells are automatically resized. The order of placing the components in a cell is determined as they are added.

When to Use GridLayout?

We can use the GridLayout in the following cases in Java:

  • When we need a simple, uniform grid and quick implementation.
  • All components should be of equal size.
  • When the layout is relatively static.

Example of GridLayout

Below is an example of a GridLayout showing 4 buttons on a grid in Java:

import java.awt.*;
import javax.swing.*;
public class GridLayoutTest{
   GridLayoutTest() {
      JFrame frame = new JFrame("GridLayout Test");
      JButton button1, button2, button3, button4;
      button1 = new JButton("Button 1");
      button2 = new JButton("Button 2");
      button3 = new JButton("Button 3");
      button4 = new JButton("Button 4");
      frame.add(button1);
      frame.add(button2);
      frame.add(button3);
      frame.add(button4);
      frame.setLayout(new GridLayout(2,2));
      frame.setSize(300,300);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
   }
   public static void main(String[] args) {
      new GridLayoutTest();
  }
}

Output

Java GridBagLayout

A GridBagLayout extends the capabilities of the GridLayout. GridBagLayout places component in each individual cell in a grid and also allows the component to span to multiple columns or rows.

Syntax

The following is the syntax for GridBagLayout initialization:

JPanel panel = new JPanel(new GridBagLayout());
GridBagConstraints gridconst= new GridBagConstraints();

In order to use GridBagLayout, we need to create a GridBagConstraints object and fill appropriate properties. Each GridBagLayout object maintains a dynamic, rectangular grid of cells, with each component occupying one or more cells called the Component display area.

When to Use GridBagLayout?

We can use the GridBagLayout in the following cases in Java:

  • When we need complex, irregular layouts or components that require different sizes.
  • Precise control over resizing is needed, and cell spanning is necessary.
  • When we need to align components across containers.

Example of GridBagLayout

Below is an example of a GridBagLayout to show 3 buttons in each respective cell in Java:

import javax.swing.*;
import java.awt.*;
public class GridBagLayoutTest extends JFrame {
   public GridBagLayoutTest() {
      setTitle("GridBagLayout Test");
      setLayout(new GridBagLayout());
      GridBagConstraints gbc = new GridBagConstraints();
      gbc.gridx = 5;
      gbc.gridy = 0;
      add(new JButton("Button1"), gbc);
      gbc.gridx = 0;
      gbc.gridy = 5;
      add(new JButton("Button2"), gbc);
      gbc.gridx = 2;
      gbc.gridy = 4;
      add(new JButton("Button3"), gbc);
   }
   public static void main(String[] args) {
      GridBagLayoutTest gbcTest = new GridBagLayoutTest();
      gbcTest.setSize(300,300);
      gbcTest.setVisible(true);
      gbcTest.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }
}

Output

Difference between GridLayout and GridBagLayout

The following are the key differences between a GridLayout and a GridBagLayout in Java:

Criteria GridLayout GridBagLayout
Complexity Simple Complex
Component Sizing Uniform Customizable per component
Cell Spanning Not supported Supported (gridwidth/gridheight)
Resizing Behavior All components resize equally Custom weights control resizing
Performance  Faster Slower
Updated on: 2025-04-29T19:16:31+05:30

7K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements