Using a GridLayout Manager : GridLayout « Swing « Java Tutorial






Place components in a grid

GridLayout()   Creates a grid layout manager 
    
    GridLayout(int rows, int cols)
    GridLayout(int rows, int cols, int hgap, int vgap)
Using a GridLayout Manager
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class TryGridLayout {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Grid Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    GridLayout grid = new GridLayout(3, 4, 30, 20);
    Container content = aWindow.getContentPane(); 
    content.setLayout(grid); 
    JButton button = null;
    for (int i = 1; i <= 10; i++) {
      content.add(button = new JButton(" Press " + i));
    }
    aWindow.pack();
    aWindow.setVisible(true);
  }
}








14.90.GridLayout
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