Updated June 30, 2023
Introduction to Grid Layout in Java
Back in the days when most programs were based on Command Line Interface, it didn’t really matter how large or small the user’s display was. But as GUI came into popularity and Operating systems provided the ability to resized apps on the screen, we have found that we need to design programs that can adapt to the window size accordingly. Here in this topic, we are going to learn about Grid Layouts in Java.
Grid Layout is one such system that lets us arrange content inside a window in a way that the represented data is not messed up when the window is made smaller or larger.
What is Grid Layout?
Grid Layout for windows is made in Java using the Grid Layout Class. It represents a layout manager that can have a programmer-specified number of columns and rows inside a rectangular grid in the window. We divide each Grid Layout Container into rectangles, each having the same size. Later, we can place one of the components of the app into each rectangle in the grid layout. Each component will fill its entire cell itself.
Once the Grid and its cell contents have been laid out, when the user changes the size of the container, the rectangles inside it change their size accordingly.
Below are the constructors for a Grid Layout class:
- GridLayout(): This constructor can be used to create a grid layout with a single column per component in a single row.
- GridLayout( int rw, int cl): Here, rw is the number of rows and cl represents the number of columns in the grid. Once used, the constructor will create a grid with the specified number of columns and row.
Here, you can set the number of rows or columns to be zero, which will mean that placement of any number of objects will be possible in the row or column. Keep in mind that you can’t set both, the number of rows and number of columns, to be zero.
- GridLayout( int rw, int cl, int hgap, int vgap): Here, rw and cl are the same earlier. hgap is the horizontal gap, and vgap is used for the vertical gap. Once used, this constructor will create a grid layout with the user-specified rows and columns. These rows and columns will have the gaps given in the constructor between them too.
Here, the vertical gap will be placed between rows and at the top and bottom edge. The horizontal gap will be placed between the columns and at the left and right edges.
Just as earlier, you can have the number of rows or columns specified as zero, but not both.
Class Methods
Now that we have taken a look at constructors, let’s take a look at some of the more commonly used class methods:
- void addLayoutComponent( String name, Component comp): This method adds the specified component alongside with the specified name to the grid layout in the program.
- int getColumns(): This method results in the number of columns in the grid layout.
- int getRows(): Similar to getColumns() method, we can use it to get the number of rows present in the grid layout.
- int getVgap(): getVgap() method is used to find the vertical gap between the components of the grid.
- int getHgap(): Results with the horizontal gap between components of the grid.
- void setVgap (int verticalgap): The vertical gap between components can be set with setVgap() class method.
- Void setColumns( int columns): This is used to assign the number of columns in the layout.
- Void setRows ( int rows): Similar to the setColumns(), this is used to set the number of rows in the grid layout.
- void setVgap( int vgap): This is used to specify the vertical gap between the components.
- void layoutContainer(Container pr): This method can lay out the specified container.
- Dimension preferredLayoutSize(Container pr): This is used to set a preferred size of containers argument using the grid layout.
- Dimension minimumLayoutSize(Container pr): This is used to set the minimum size of the container argument using the grid layout.
- void removeLayoutComponent(Component compo): This class method can be used to remove the specified component from the grid layout.
- String toString() : toString() returns the values of grid layout in a string representation.
Example of a Java Program using Grid Layout
Let us see with the help of an example:
package com.exmplegrid.gui;
import java.awt.*;
import java.awt.event.*;
public class GridLayoutExample {
private Frame themainframe;
private Label headLabel;
private Label lable_Status;
private Panel control_panel;
private Label messsagelabel;
public GridLayoutExample()
{
prepareGUI();
}
public static void main(String[] args) {
GridLayoutExample gridLayoutExample = new GridLayoutExample();
gridLayoutExample.showGridLayoutDemo();
}
private void prepareGUI() {
themainframe = new Frame("Java Grid Examples");
themainframe.setSize(500,500);
themainframe.setLayout(new GridLayout(3, 1));
themainframe.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent) {
System.exit(0);
}
} );
headLabel = new Label();
headLabel.setAlignment(Label.CENTER);
lable_Status = new Label();
lable_Status.setAlignment(Label.CENTER);
lable_Status.setSize(350,100);
messsagelabel = new Label();
messsagelabel.setAlignment(Label.CENTER);
messsagelabel.setText(" This is an example of Grid Layout in Java.");
control_panel = new Panel();
control_panel.setLayout(new FlowLayout());
themainframe.add(headLabel);
themainframe.add(control_panel);
themainframe.add(lable_Status);
themainframe.setVisible(true);
}
private void showGridLayoutDemo() {
headLabel.setText("GridLayout");
Panel panel = new Panel();
panel.setBackground(Color.white);
panel.setSize(300,300);
GridLayout layout = new GridLayout(0,3);
layout.setHgap(10);
layout.setVgap(10);
panel.setLayout(layout);
panel.add(new Button(“ This is the button 1"));
panel.add(new Button(“ This is the button 2"));
panel.add(new Button(“ This is the button 3"));
panel.add(new Button(“ This is the button 4"));
panel.add(new Button(“ This is the button 5"));
control_panel.add(panel);
themainframe.setVisible(true);
}
}
Conclusion
Being able to create windows that can adapt to resizing automatically will help you in making sure that the java program will work on any screen size. The grid Layout class provides an easy way of getting started with this and laying out objects properly in the cells.
Recommended Articles
This has been a guide to Grid Layout in Java. Here we discussed the constructors, class methods and the examples of Grid Layout in Java. You can also go through our other suggested articles to learn more –