The VariableGridLayout class is a layout manager
that lays out a container's components in a rectangular grid
with variable cell sizes.
The container is divided into rectangles, and one component is placed
in each rectangle. Each row is as large as the largest component in
that row, and each column is as wide as the widest component in
that column.
This behavior is basically the same as in
java.awt.GridLayout , but with different row heights and
column widths for each row/column.
For example, the following is an applet that lays out six buttons
into three rows and two columns:
import java.awt.*;
import java.applet.Applet;
public class ButtonGrid extends Applet {
public void init() {
setLayout(new VariableGridLayout(VariableGridLayout.FIXED_NUM_COLUMNS, 2));
add(new Button("1"));
add(new Button("2"));
add(new Button("3"));
add(new Button("4"));
add(new Button("5"));
add(new Button("6"));
}
}
Programmer's remark: VariableGridLayout could be faster, if it would
reside in the package java.awt, because then it could access some
package private fields of Container or
Component . Instead, it has to call
Component.getSize() ,
which allocates memory on the heap.
Todo:
- Use alignmentX/Y property if the grid cell is larger than the preferred size of the component.
- Ability to span components over more than one cell horizontally
author: Dirk Moebius version: 1.0 See Also: java.awt.GridLayout |