CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Swing > GridLayout in Java Swing

Swing Tutorial

  • What Is Java Swing
  • First Program
  • What is AWT
  • Advantages of Swing Over AWT
  • JFrame
  • Swing Components
  • JLabel
  • JTextField
  • JPasswordField
  • JButton
  • Event Handling
  • FlowLayout
  • BorderLayout
  • GridLayout
  • GridBagLayout
  • BoxLayout
  • JPanel
  • JCheckBox
  • JRadioButton
  • Border Component
  • JComboBox
  • JList
  • JList with MVC
  • Mouse Event
  • Key Event
  • JMenuBar
  • JTextArea
  • Using Color
  • Use Font
  • Display Image/Icon
  • Dialog Box
  • JTable
  • JDesktopPane and JInternalFrame
  • JDesktopPane
  • JInternalFrame
  • Adapter Classes
  • Swing Timer
  • JScrollPane
  • JSlider
  • JProgressBar
  • JSeparator
  • JToolBar
  • ActionListener
  • ItemListener
  • Worker Thread
  • Nimbus Look and Feel

GridLayout in Java Swing

GridLayout in java swing

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Published: March 9, 2021 · 1 min read · 0 Comments
Share: in X

GridLayout is a layout manager in Java Swing that arranges components in a grid of rows and columns. Each cell in the grid can contain one component, and all cells in a row or column have the same width or height.

Following is an example of how to use GridLayout in Java Swing:

import java.awt.GridLayout;

import javax.swing.JButton;
import javax.swing.JFrame;

public class GridLayoutDemo {
	public static void main(String[] args) {
		JFrame frame = new JFrame("GridLayout Demo");
		frame.setLayout(new GridLayout(3, 3)); // set the layout manager

		// add buttons to the frame
		for (int i = 1; i <= 9; i++) {
			frame.add(new JButton(String.valueOf(i)));
		}

		frame.setSize(300, 300);
		frame.setVisible(true);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}

Output:

GridLayout demo

Related Posts:

  • MySQL Commands for Developers
  • What Is Java Swing? A Complete Guide to Java’s GUI Toolkit
  • Scrollable ResultSet in JDBC
  • Java Swing Tutorial
  • Processing Results with ResultSet in JDBC
  • Control Statements in Java
Tags:javaswing
Was this article helpful?
← Previous ArticleAdapter Classe in Java Swing
Next Article →GridBagLayout in Java Swing

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
  • Complete Guide to JaCoCo: How to Measure Java Code Coverage Accurately
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
© 2026 CoderSathi. All rights reserved. Privacy Policy · Sitemap