Java Swing How to - Layout JButton as table with GridLayout








Question

We would like to know how to layout JButton as table with GridLayout.

Answer

//from www  . ja  v a 2  s.c  o  m
import java.awt.GridLayout;

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

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    JPanel panel = new JPanel(new GridLayout(10, 10, 10, 10));

    for (int i = 0; i < 100; i++) {
      panel.add(new JButton(String.valueOf(i)));
    }

    frame.add(panel);

    frame.setSize(600, 600);
    frame.setVisible(true);
  }
}