Considered as one of the simplest layouts to implement, Grid Layout is all about placing your swing components in a Grid (Rectangular grid).
GridLayout Class has three constructors as follows:-

  1. GridLayout() :- Allows you to create a grid layout having just a single column .
  2. GridLayout(int row_count,into column_count) :- Creates a GridLayout as per the row_count and the column _column count supplied to it .
  3. GridLayout(int row_count,int column_count,int horizontal_gap,int vertical_gap):- Creates a GridLayout as per the user inputted row_count,column_count,horizontal_gap and the vertical_gap.
Let’s look at an example JFrame form making the use of Grid Layout:-
package layouts;
import java.awt.GridLayout;
import javax.swing.*;
/**
*
* @author Pramod | http://techwayz.com
*/
public class GridLayoutDemo {
public static void main(String args[]) {
JFrame jf = new JFrame();
JPanel jp = new JPanel();
JPanel apanel = new JPanel();
JPanel buttonBox = new JPanel();
JPanel cbPanel = new JPanel();
jf.setTitle(“Grid Layout example”);
GridLayout gl = new GridLayout(4, 1);
jf.setLayout(gl);
jp.add(new JLabel(“Name”));
jp.add(new JTextField(20));
apanel.add(new JLabel(“Addrs”));
apanel.add(new JTextField(20));
cbPanel.add(new JLabel(“Sex ”));
cbPanel.add(new JComboBox(new Object[]{“Male”, “Female”}));
cbPanel.add(new JLabel(“Blood Group”));
cbPanel.add(new JComboBox(new Object[]{“O+”, “AB+”}));
buttonBox.add(new JButton(“Add”));
buttonBox.add(new JButton(“Update”));
jf.add(jp);
jf.add(apanel);
jf.add(cbPanel);
jf.add(buttonBox);
jf.setSize(300, 180);
jf.setLocation(300, 400);
jf.setResizable(false);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.setVisible(true);
}
}
Screenshot (When you’ll run the above code) :-

 

Description:-
GridLayout gl = new GridLayout(4, 1):- In this line , we are passing row_count and the column_count to the GridLayout Constructor i.e 4 and 1 . So that Grid Layout class would setup and initialize the necessary code to create a rectangular Grid Layout with 1 column and 4 rows to hold our 4 different JPanels (JPanels containing the name , address, comboboxes and the button components)
I’ve used a single column and 4 different rows because I want my 4 JPanels to be placed one after the another (In 4 different rows)
If you want you can specify the Horizontal and vertical gaps between your swing/awt components by using the setHgap() and the setVgap() methods .
jf.setLayout(gl) :- Without this line , you won’t be able to use the Grid Layout , this line tells the Layout Manager of your class to change your Layout from the default Layout to the one you’ve specified as a Parameter in the setLayout() method .
jf.add(jp) :- This line will place our name components of our form (i.e name JLabel and the associated text field) in the first row …as out of all the elements … we are adding the name components first .
jf.add(apanel):- This line will place our name components of our form (i.e name JLabel and the associated text field) in the second row our JFrame .
jf.add(cbPanel):- This line will place our name components of our form (i.e name JLabel and the associated text field) in the third row our JFrame …
jf.add(buttonBox):- :- This line will place our name components of our form (i.e name JLabel and the associated text field) in fourth row of our JFrame…
the author

I am a post graduate in computer sciences .I have 1+ year experience of working as a Java/J2EE developer in a top notch IT company. I love developing applications and writing technology related articles .