- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a GridLayout with rows and columns in Java?
While creating a GridLayout, you need to set the rows and columns as parenthesis. A GridLayout is used to create a layout the specified number of rows and columns.
Let’s say we have a GridLayout, with 1 row and 4 columns −
GridLayout layout = new GridLayout(1,4);
The following is an example to create a GridLayout with rows and columns −
Example
package my; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Font; import java.awt.GridLayout; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JTextArea; import javax.swing.WindowConstants; public class SwingDemo { public static void main(String[] args) { JFrame frame = new JFrame("Sections"); JPanel panel = new JPanel(); panel.setBackground(Color.orange); GridLayout layout = new GridLayout(1,4); layout.setHgap(40); panel.setLayout(layout); panel.add(new JButton("Overview")); panel.add(new JButton("Samples")); panel.add(new JButton("Tutorials")); panel.add(new JButton("Support")); frame.add(panel); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setSize(500, 300); frame.setVisible(true); } }
Output
- Related Articles
- Java program to create three vertical columns with equal number of buttons in GridLayout
- How to create vertical button column with GridLayout in Java?
- How to multiply a matrix columns and rows with the same matrix rows and columns in R?
- How to create table rows & columns in HTML?
- How to set vertical gap between elements in a GridLayout with Java?
- How to set horizontal gap between elements in a GridLayout with Java?
- How to get rows and columns of 2D array in Java?
- How to create an empty data frame with fixed number of rows and without columns in R?
- How to create an empty DataFrame and append rows & columns to it in Pandas?
- How to display JTextArea in the form of a table with GridLayout in Java?
- How to create a subset of rows or columns of a matrix in R?
- How to align rows in a Matplotlib legend with 2 columns?
- How to layout table cells, rows, and columns with JavaScript?
- How to get the number of rows and columns of a JTable in Java Swing
- How to create a matrix with equal rows in R?

Advertisements