GridLayout

The GridLayout manager arranges components into rows and columns. You fill the grid row-by-row with graphical components. For example, the first element to be added to a 3 row x 2 column grid will be the first element of the first row. The third element to be added to a 3x2 grid will be the first element of the second row. The following applet lays out 6 buttons in a 3x2 grid pattern:



Corresponding source code:

import java.awt.*;
import java.applet.Applet;
public class GridLayoutTest extends Applet {
    public void init() {
        setLayout(new GridLayout(3,2));
        add(new Button("1")); // uppper left button
        add(new Button("2"));
        add(new Button("3"));
        add(new Button("4"));
        add(new Button("5"));
        add(new Button("6")); // lower right button
    }
}

See also How to Use GridLayout.

Related Magercise(s):