GridLayout

Java GridLayout Example

Photo of Katerina ZamaniKaterina ZamaniJanuary 29th, 2014Last Updated: September 16th, 2020
0 1,107 2 minutes read

The grid layout Java represents a layout manager with a specified number of rows and columns. Every rectangle cell at Gridlayout java 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 contain simple components.

1. Syntax of the Java GridLayout

GridLayout has two constructors:

The arguments that are used are:

Grid Layout example
  • 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. Grid Layout Java example

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

CreateGridLayoutTest.java file and paste the following code.

GridLayoutTest.java

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
packagecom.javacodegeeks.desktop.gridlayout;
 
importjava.awt.GridLayout;
importjavax.swing.JButton;
importjavax.swing.JFrame;
importjavax.swing.JTextArea;
 
publicclassGridLayoutTest {
 
    privatestaticJButton[] arrayBtn;
     
    publicstaticvoidmain(String[] args) {
         
        // the frame that contains the components
        JFrame frame =newJFrame("GridLayoutTest from JCG");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 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 =newGridLayout(5,3,10,10);
        // what layout we want to use for our frame
        frame.setLayout(grid);
         
        // add a text field with a specified text to the frame
        JTextArea text =newJTextArea();
        text.setText("Result");
        text.setEditable(false);
        frame.add(text);
         
        // add buttons to the frame
        frame.add(newJButton("+"));
        frame.add(newJButton("="));
 
        arrayBtn =newJButton[10];
        // add JButtons dynamically
        for(inti=0; i < arrayBtn.length; i++) {
            arrayBtn[i] =newJButton(Integer.toString(i));
            frame.add(arrayBtn[i]);
        }
        
        frame.setVisible(true);
 
    }
 
}

Lets explain the code above. After we instantiate and setJframe, we create theGridLayout by declaring its rows, columns and its horizontal and vertical gaps. After that we set the instantiatedGridLayout as the layout of our container by callingsetLayout() method. In order to include the components to our layout, we have to add them to theJFrame and not to theGridLayout. In our example we add some components (JtextArea andJButton) directly to the frame and some others dynamically, as an array of that type (array ofJButtons).

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

Java GridLayout - GridLayoutTest1
GridLayoutTest1

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

3. Download the source code

This was an example of Grid Layout in Java.

Download
Download the source code of this example:Java GridLayout Example

Last updated on May 8th, 2020

Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
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 ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Katerina ZamaniKaterina ZamaniJanuary 29th, 2014Last Updated: September 16th, 2020
0 1,107 2 minutes read
Photo of Katerina Zamani

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.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.