Open In App

Java AWT | GridLayout Class

Last Updated : 21 Aug, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

GridLayout class represents a layout manager with a specified number of rows and columns in a rectangular grid. The GridLayout container is divided into an equal-sized of rectangles, and one of the components is placed in each rectangle. Every rectangle cell has the same size therefore, they contain a component, which fills the entire cell. When the user changes or adjusts the size of the container, the size of each rectangles changes accordingly.

Constructors of the class:

  1. GridLayout(): It Creates a grid layout with a default of one column per component, in a single row.
  2. GridLayout(int rw, int cl): It creates a grid layout with the specified number of rows and columns.
  3. GridLayout(int rw, int cl, int hgap, int vgap): It creates a grid layout with the specified number of rows and columns with horizontal and vertical gap.

Commonly Used Methods:

  • addLayoutComponent(String str, Component cmp): Adds the specified component with the specified name to the layout.
  • setColumns(int cl): Sets the number of columns in this layout to the specified value.
  • setHgap(int hgap): Sets the horizontal gap between components to the specified value.
  • setRows(int rw): Sets the number of rows in this layout to the specified value.
  • setVgap(int vgap): Sets the vertical gap between components to the specified value.
  • layoutContainer(Container pr): Lays out the specified container using this layout.
  • toString(): Returns the string representation of this grid layout’s values.

Below programs illustrate the GridLayout class:

  • Program 1: In below program we are passing the argument in GridLayout. We create 4 JLabel components named “one“, “two“, “three“, “four” and create 2 JButton components named “buttonsave” and “buttonexit” and create 4 Jtextfield components named “tname“, “tcode“, “tdesig“, “tsalary” and all of add them to the JFrame by the method add(). We will set the visibility and size of the frame by using setVisible() and setSize() method. The layout is set by using setLayout() method.




    // Java program to illustrate the GridLayout
    import javax.swing.*;
    import java.awt.*;
      
    // class GridLayout extends JFrame
    public class GridLayoutDemo extends JFrame {
      
    GridLayoutDemo() {
      
        // Creating Object P1 of JPanel class
        JPanel p1 = new JPanel();
      
        // set the layout
        p1.setLayout(new GridLayout(4, 2));
      
        // Creating Object of "FlowLayout" class
        FlowLayout layout = new FlowLayout();
      
        // Creating Object P2 of JPanel class
        JPanel p2 = new JPanel();
      
        // set the layout
        p2.setLayout(layout);
      
        // Declaration of objects of JLabel class.
        JLabel one, two, three, four;
      
        // Declaration of objects of JTextField class.
        JTextField tname, tsalary, tcode, tdesig;
      
        // Declaration of objects of JButton class.
        JButton buttonSave, buttonExit;
      
        // Initialization of object 
        // "one" of JLabel class.
        one = new JLabel("NAME");
      
        // Initialization of object 
        // "tname" of JTextField class.
        tname = new JTextField(20);
      
        // Initialization of object
        // "two" of JLabel class.
        two = new JLabel("CODE");
      
        // Initialization of object 
        // "tcode" of JTextField class.
        tcode = new JTextField(20);
      
        // Initialization of object
        // "three" of JLabel class.
        three = new JLabel("DESIGNATION");
      
        // Initialization of object 
        // "tdesig" of JTextField class.
        tdesig = new JTextField(20);
      
        // Initialization of object
        // "four" of JLabel class.
        four = new JLabel("SALARY");
      
        // Initialization of object 
        // "tsalary" of JTextField class.
        tsalary = new JTextField(20);
      
        // Initialization of object
        // "buttonsave" of JButton class.
        buttonSave = new JButton("SAVE");
      
        // Initialization of object
        // "buttonexit" of JButton class.
        buttonExit = new JButton("EXIT");
      
        // Adding Jlabel "one" on JFrame.
        p1.add(one);
      
        // Adding JTextField "tname" on JFrame.
        p1.add(tname);
      
        // Adding Jlabel "two" on JFrame.
        p1.add(two);
      
        // Adding JTextField "tcode" on JFrame.
        p1.add(tcode);
      
        // Adding Jlabel "three" on JFrame.
        p1.add(three);
      
        // Adding JTextField "tdesig" on JFrame.
        p1.add(tdesig);
      
        // Adding Jlabel "four" on JFrame.
        p1.add(four);
      
        // Adding JTextField "tsalary" on JFrame.
        p1.add(tsalary);
      
        // Adding JButton "buttonsave" on JFrame.
        p2.add(buttonSave);
      
        // Adding JButton "buttonexit" on JFrame.
        p2.add(buttonExit);
      
        // add the p1 object which 
        // refer to the Jpanel
        add(p1, "North");
      
        // add the p2 object which
        // refer to the Jpanel
        add(p2, "South");
      
        // Function to set visible
        // status of JFrame.
        setVisible(true);
      
        // this Keyword refers to current
        // object. Function to set size of JFrame.
        this.setSize(400, 180);
    }
      
        // Main Method
        public static void main(String args[])
        {
      
            // calling the constructor
            new GridLayoutDemo();
        }
    }

    
    

    Output:


  • Program 2: In below program we are passing the argument in GridLayout. We create 5 JButton components named “btn1“, “btn2“, “btn3“, “btn4“, “btn5” and then add them to the JFrame by the method add(). We will set the visibility and size of the frame by using setvisible() and setsize() method. The layout is set by using setLayout() method.




    // Java program to illustrate the GridLayout
    import java.awt.*;
    import javax.swing.*;
      
    // create a class griddemo
    public class Griddemo {
      
    // Main Method
    public static void main(String[] args)
    {
      
        // Creating Object of JFrame class 
        // with new name frame
        JFrame frame = new JFrame("GridLayout Demo");
      
        // Initialization of object 
        // "btn1" of JButton class.
        JButton btn1 = new JButton("Button 1");
      
        // Initialization of object 
        // "btn2" of JButton class.
        JButton btn2 = new JButton("Button 2");
      
        // Initialization of object 
        // "btn3" of JButton class.
        JButton btn3 = new JButton("Button 3");
      
        // Initialization of object
        // "btn4" of JButton class.
        JButton btn4 = new JButton("Button 4");
      
        // Initialization of object 
        // "btn5" of JButton class.
        JButton btn5 = new JButton("Button 5");
      
        // Creating Object Panel of JPanel class
        // create grid layout with 3 rows, 
        // 2 columns with horizontal and 
        // vertical gap set to 10
        JPanel panel = new JPanel(new GridLayout(3, 2, 10, 10));
      
        // Adding JButton "btn1" on JPanel.
        panel.add(btn1);
      
        // Adding JButton "btn2" on JPanel.
        panel.add(btn2);
      
        // Adding JButton "btn3" on JPanel.
        panel.add(btn3);
      
        // Adding JButton "btn4" on JPanel.
        panel.add(btn4);
      
        // Adding JButton "btn5" on JPanel.
        panel.add(btn5);
      
        // Function to close the operation of JFrame.
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      
        // Function to set size of JFrame.
        frame.setSize(300, 150);
      
        // Function to get the content of JFrame.
        frame.getContentPane().add(panel);
      
        // Function to set visible status of JFrame.
        frame.setVisible(true);
    }
    }

    
    

    Output:


Note: The above programs might not run in an online IDE. Please use an offline compiler.

Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/GridLayout.html



Similar Reads

Java.awt.image.RescaleOp Class in Java with Examples
RescaleOp is a class in java.awt.image package which implements BufferedImageOp and RasterOp interface. This class performs a pixel-by-pixel rescaling of the data in the source image by multiplying the sample values for each pixel by a scale factor and then adding an offset. The scaled sample values are clipped to the minimum/maximum representation
5 min read
Java AWT vs Java Swing vs Java FX
Java's UI frameworks include Java AWT, Java Swing, and JavaFX. This plays a very important role in creating the user experience of Java applications. These frameworks provide a range of tools and components for creating graphical user interfaces (GUIs) that are not only functional but also visually appealing. As a Java developer, selecting the righ
11 min read
Java AWT | Cursor class with examples
Cursor class is a part of Java AWT package and it is used to create custom cursors or inherit system or predefined cursors. Cursor class is mainly used to encapsulate the bitmap representation of the mouse cursor. Constructor of cursor class are : Cursor(int t) : Creates a cursor with specified class Cursor(String name) : Creates a custom cursor wi
4 min read
Java AWT | Choice Class
Choice class is part of Java Abstract Window Toolkit(AWT). The Choice class presents a pop- up menu for the user, the user may select an item from the popup menu. The selected item appears on the top. The Choice class inherits the Component. Constructor for the Choice class Choice() : creates an new empty choice menu . Different Methods for the Cho
5 min read
Java AWT | Desktop Class
The Desktop class is a part of Java AWT package. This class is used to launch associated applications registered on the native desktop to handle a URI or a file. Important Points About Desktop Class : It can open a default web browser showing a specific URI It can launch default mail client with optional mailto URI It can launch a registered applic
5 min read
Java AWT | Color Class
The Color class is a part of Java Abstract Window Toolkit(AWT) package. The Color class creates color by using the given RGBA values where RGBA stands for RED, GREEN, BLUE, ALPHA or using HSB value where HSB stands for HUE, SATURATION, BRIcomponents. The value for individual components RGBA ranges from 0 to 255 or 0.0 to 0.1. The value of alpha det
9 min read
Java AWT | Dimension Class
Dimension class is a part of Java AWT. It contains the height and width of a component in an integer as well as double precision. The use of Dimension class is that many functions of Java AWT and Swing return dimension object. Constructors of the Dimension class Dimension() : It will create a new Object with height and width set to zero.Dimension(D
4 min read
Java AWT | Canvas Class
Canvas class is a part of Java AWT. Canvas is a blank rectangular area where the user can draw or trap input from the user. Canvas class inherits the Component class.Constructor of the Canvas class are : Canvas(): Creates a new blank canvas.Canvas(GraphicsConfiguration c): Creates a new canvas with a specified graphics configuration. Commonly used
3 min read
Java AWT | BorderLayout Class
BorderLayout is the default layout for the window objects such as JFrame, JWindow, JDialog, JInternalFrame etc. BorderLayout arranges the components in the five regions. Four sides are referred to as north, south, east, and west. The middle part is called the center. Each region can contain only one component and is identified by a corresponding co
4 min read
Java AWT | CardLayout Class
The CardLayout class manages the components in such a way that only one component is visible at a time. It treats each component as a card in the container. Only one card is visible at a time, and the container acts as a stack of cards. The first component added to a CardLayout object is the visible component when the container is first displayed.
6 min read
Article Tags :
Practice Tags :