Technical Support
Discussion Forum
Online Training
Read About Java
Java In-Depth
Product Discounts
Membership Information

Java Cup Logo

JDC Home Page


Top
Back
Next
Online Training
shadowSearchFAQFeedback

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, 2 column grid will be the first element of the first row. The third element to be added to a 3-row, 2-column grid will be the first element of the second row. The following applet lays out six buttons in a 3-row, 2-column 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 exercise(s):