Creating Rows of Buttons : GridLayout « Swing « Java Tutorial

Creating Rows of Buttons
import java.awt.Container;
import java.awt.GridLayout;

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

public class GridTest extends JFrame {

  public static void main(String[] args) {
    int rows = 2;
    int cols = 3;
    GridTest gt = new GridTest(rows, cols);

  public GridTest(int rows, int cols) {
    Container pane = getContentPane();
    pane.setLayout(new GridLayout(rows, cols));
    for (int i = 0; i < 20; i++) {
      JButton button = new JButton(Integer.toString(i + 1));


14.90.1.Laying Out Components in a Grid
14.90.2.How to Use GridLayoutHow to Use GridLayout
14.90.3.Sample GridLayout ApplicationSample GridLayout Application
14.90.4.Creating Rows of ButtonsCreating Rows of Buttons
14.90.5.Uses a 2-column grid.Uses a 2-column grid.
14.90.6.Use a 1x1 grid to make a component as large as possibleUse a 1x1 grid to make a component as large as possible
14.90.7.Using a GridLayout ManagerUsing a GridLayout Manager