Katerina Zamani

About Katerina Zamani

Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.

Java GridLayout Example

GridLayout represents a layout manager with a specified number of rows and columns. Every rectangle cell has the same size and contains a component, which fills in the entire size of the cell.

In this example we are going to design a simple calculator using GridLayout, where the grid will contains simple components.


1. Syntax of GridLayout

GridLayout has two constructors:

The arguments that are used are:

  • rows: is the number of rows in the grid.
  • cols: represents the number of columns in the grid.
  • hgap: is the horizontal space between the cells.
  • vgap: is the vertical space between the cells

2. Example of GridLayout

Before showing how to implement a GridLayout, it is important to mention that we should initialize a Jframe, which is the window that contains the components.

Create GridLayoutTest.java file and paste the following code.


package com.javacodegeeks.desktop.gridlayout;

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;

public class GridLayoutTest {

	private static JButton[] arrayBtn;
	public static void main(String[] args) {
		// the frame that contains the components
		JFrame frame = new JFrame("GridLayoutTest from JCG");
	    // set the size of the frame
	    frame.setSize(350, 350);
	    // set the rows and cols of the grid, as well the distances between them
	    GridLayout grid = new GridLayout(5, 3, 10, 10);
	    // what layout we want to use for our frame
	    // add a text field with a specified text to the frame
	    JTextArea text = new JTextArea();
        // add buttons to the frame
	    frame.add(new JButton("+"));
	    frame.add(new JButton("="));

	    arrayBtn = new JButton[10];
	    // add JButtons dynamically
	    for(int i=0; i < arrayBtn.length; i++) {
	    	arrayBtn[i] = new JButton(Integer.toString(i));



Lets explain the code above. After we instantiate and set Jframe, we create the GridLayout by declaring its rows, columns and its horizontal and vertical gaps. After that we set the instantiated GridLayout as the layout of our container by calling setLayout() method. In order to include the components to our layout, we have to add them to the JFrame and not to the GridLayout. In our example we add some components (JtextArea and JButton) directly to the frame and some others dynamically, as an array of that type (array of JButtons).

The execution of this example is shown in the next image.


You can notice that the orientation of the components is horizontal and left-to-right, because that is the default value of the ComponentOrientation property.

Download the source code

This was an example of GridLayout in Java. Download the source code of this example: GridLayoutTest.zip

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

Examples Java Code Geeks and all content copyright © 2010-2015, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Examples Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Examples Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.