Grid Layout in Java With Examples

Grid Layout With Examples:


logo

Table of Contents

a) Introduction to Grid Layout
b) Different Constructors of Grid Layout
c) Source Code of each Constructor
d) Output of each Constructor
************************

Introduction:

Grid Layout is the layout manager in which the components are arranged in the matrix form i.e row and column. Grid Layout is mostly and commonly used in gallery based application in which components are arranged in row and columns. The size of the components cannot be altered and it remains static.

There are three constructors of Grid Layout, they are:
     GridLayout()
          GridLayout(int row, int column)
         GridLayout(int row, int column, int horizontal_gap, int vertical_gap)

A) GridLayout(): It creates the infinite numbers of column because here we cannot specify the rows and column. Let’s see the example,

/*
 A SWING GUI TO DEMONSTRATE THE GRID LAYOUT
 */
package LayoutManagers;

/**
 *
 * @author AnkitPC
 */
//importing required packages
import javax.swing.*; //for components
import java.awt.*; //for layout manager
public class GridLayoutDemo {
    JFrame f;
    JButton b1,b2,b3,b4;
    //constructor
    GridLayoutDemo(){
        f=new JFrame("Grid Layout Demo");
        f.setSize(400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        f.setLayout(new GridLayout()); //first constructor of grid layout
       
        b1=new JButton("One");
        b2=new JButton("Two");
        b3=new JButton("Three");
        b4=new JButton("Four");
       
        //add the component to the frame
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
       
        f.setVisible(true);
    }
   
    //main method
    public static void main(String[] args) {
        //call the constructor
        new GridLayoutDemo();
    }   
}

The above source code demonstrate the first constructor of the Grid Layout. In the output below that we can see the number of columns and only one row. The first constructor of Grid Layout adds the infinite number of columns int the row. Also there is no any horizontal and vertical gap between the components


Output:



B) GridLayout(int row, int column): It creates the specified numbers of rows and columns in the frame/container. It has two parameters in which we can pass the integer value. Let’s see the example;

/*
A SWING GUI TO DEMONSTRATE THE GRID LAYOUT

 */
package LayoutManagers;

/**
 *
 * @author AnkitPC
 */
//importing required packages
import javax.swing.*; //for components
import java.awt.*; //for layout manager
public class GridLayoutDemo {
    JFrame f;
    JButton b1,b2,b3,b4;
    //constructor
    GridLayoutDemo(){
        f=new JFrame("Grid Layout Demo");
        f.setSize(400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        f.setLayout(new GridLayout(2,2)); //second constructor of grid layout
       
        b1=new JButton("One");
        b2=new JButton("Two");
        b3=new JButton("Three");
        b4=new JButton("Four");
       
        //add the component to the frame
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
       
        f.setVisible(true);
    }
   
    //main method
    public static void main(String[] args) {
        //call the constructor
        new GridLayoutDemo();
    }
   
   
}

The above source code demonstrate the second constructor of the Grid Layout. As we can see in the output that there is two rows and two columns. It is because that we can specify its number of rows and columns through its constructor. Also there is no any horizontal and vertical gap between the components

Output:



C) GridLayout(int row, int column, int horizontal_gap, int vertical_gap): It creates the specified numbers of rows and column and also we can specify its horizontal and vertical gap between the components. Let’s see the example;

/*
A SWING GUI TO DEMONSTRATE THE GRID LAYOUT

 */
package LayoutManagers;

/**
 *
 * @author AnkitPC
 */
//importing required packages
import javax.swing.*; //for components
import java.awt.*; //for layout manager
public class GridLayoutDemo {
    JFrame f;
    JButton b1,b2,b3,b4;
    //constructor
    GridLayoutDemo(){
        f=new JFrame("Grid Layout Demo");
        f.setSize(400,300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       
        f.setLayout(new GridLayout(2,2,5,5)); //third constructor of grid layout
       
        b1=new JButton("One");
        b2=new JButton("Two");
        b3=new JButton("Three");
        b4=new JButton("Four");
       
        //add the component to the frame
        f.add(b1);
        f.add(b2);
        f.add(b3);
        f.add(b4);
       
        f.setVisible(true);
    }
   
    //main method
    public static void main(String[] args) {
        //call the constructor
        new GridLayoutDemo();
    }
   
   
}


The above source code demonstrate the third constructor of the Grid Layout. As we can see in the output that there is two rows and two columns. It is because that we can specify its number of rows and columns through its constructor.  There is a horizontal and vertical gap between the components.

Output:

************

Post a Comment

0 Comments