5.3.3 Layout of Components


A layout manager allows you to specify the arrangement of components in a container in abstract terms such as "next to each other", "columnwise", "in a grid of given dimension", and so on. The AWT provides the following layout managers: If the predefined layouts are not enough for you, you must define your own layout manager(s).

Before adding components, you must first define the layout via the command setLayout(layout manager) or use the default layout manager. For a Panel (including an Applet), the default layout is FlowLayout; for a Window this is BorderLayout.

Examples will illustrate the first four layout managers.

FlowLayout

This is the default layout manager for any panel. It places components from left to right with automatic wrapping. For example, the following Java code

import java.applet.Applet;
import java.awt.*;

public class FlowTest extends Applet {

  private Button ok, cancel, help;

  public void init() {
    //  define FlowLayout manager
    setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10));
    //  define buttons
    ok = new Button("OK");
    cancel = new Button("Cancel");
    help = new Button("Help");
    //  add buttons
    add(ok);
    add(cancel);
    add(help);
  }
}
will arrange three buttons horizontally (if the applet window size is large enough), centered, and with gaps of 10 pixels between components (in both directions). See the applet below.

BorderLayout

The BorderLayout provides five zones arranged by the geographical names North, South, East, West, and Center. An example:

It is produced by the following Java code

import java.applet.Applet;
import java.awt.*;

public class BorderTest extends Applet {
  private Button left, right, top, bottom, center;

  public void init() {
    //  define BorderLayout manager
    setLayout(new BorderLayout(5,5));
    //  define buttons
    left = new Button("Left");
    right = new Button("Right");
    top = new Button("Top");
    bottom = new Button("Bottom");
    center = new Button("Center");
    //  add buttons
    add("North", top);
    add("South", bottom);
    add("East", right);
    add("West", left);
    add("Center", center);
    }

  public Insets insets() {
    //  use margins of 40 pixels
    return new Insets(40,40,40,40);
  }
}
There is a gap of 5 pixels between each button. Without this specification in the BorderLayout method, there will be no gap between components.

Talking about gaps between components in a container: you can also create margins in a container or component. Use the method

Insets(int top, int left, int bottom, int right)

as shown in the above applet (margins are in pixels).

GridLayout

A GridLayout divides a container into a specified number of rows and columns and arranges the components in those rows and columns, left-to-right and top-to-bottom. For an example, we refer to the earlier example in which we described the Checkbox component. The same example of GridLayout is part of the next one, viz. GridBagLayout.

GridBagLayout

In the GridBagLayout a container is again divided into a specified number of rows and columns, but now components may occupy more than one cell of the grid (their "display areas"). Heights of rows and widths of columns are not necessarily the same in a GridBagLayout. All this gives you more control on the size and position of components, on filling of unused space, on internal and external padding, and so on. GridBagLayout is rather complicated to use. We shall only give one example to serve as a prototype, but it does no harm if you skip it.