Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
gridlayout

What is the Java GridLayout?

Educative Answers Team

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The GridLayout class in Java is used to design layouts that have a specified number of rows and columns in a rectangular grid. Each component is placed in the equal-sized rectangle. The changes are visible for each small-rectangle whenever its size is adjusted.


Constructors

svg viewer

Common methods

Method Description
setColumns(int col) This function sets the specified number of columns for the layout.
setRows(int row) This function sets the specified number of rows for the layout.
setHgaps(int val) This function sets the horizontal gaps according to the specified value.
setVgaps(int val) This function sets the vertical gaps according to the specified value.

Example

import java.awt.*;
import javax.swing.*;
public class GridLayout{
JFrame frm;
MyGridLayout(){
frm=new JFrame();
JButton btn1=new JButton("1");
JButton btn2=new JButton("2");
JButton btn3=new JButton("3");
JButton btn4=new JButton("4");
JButton btn5=new JButton("5");
JButton btn6=new JButton("6");
frm.add(btn1);
frm.add(btn2);
frm.add(btn3);
frm.add(btn4);
frm.add(btn5);
frm.add(btn6);
frm.setLayout(new GridLayout(2,3));//creating grid layout of 2 row and 3 columns
frm.setSize(300,300);
frm.setVisible(true);
}
public static void main(String[] arguments) {
new GridLayout();
}
}

Code output

svg viewer

RELATED TAGS

java
gridlayout
Copyright ©2022 Educative, Inc. All rights reserved

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring