Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

How to use GridLayout in Java using Swing ?.

A Simple Java Program demonstrating a way to use GridLayout layout using Swing API.

package com.hubberspot.swing.example;

import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class GridLayoutDemo extends JFrame {
  public GridLayoutDemo() {
    setLayout(new GridLayout(4,3));
    for(int i = 0; i < 10; i++){
      JTextField textfield= new JTextField();
      textfield.setText("TextField " + (i));
  public static void main(String[] args) {
    setFrame(new GridLayoutDemo(), 300, 200);

  public static void 
  setFrame(final JFrame frame, final int width, final int height) {
    SwingUtilities.invokeLater(new Runnable() {
      public void run() {
 frame.setSize(width, height);

Output of the program : 

© 2021 Learn Java by Examples Template by Hubberspot