The basic metaphor adopted is that of a table, laid out as rows and columns, similar to tables in HTML. Unlike the AWT GridLayout, each row and column is sized separately, and features such as row and column spanning are supported.
For example, the following code will lay out a 2x2 table:
setLayout(new TableLayout(2));
add(new Label("Top left"));
add(new Label("Top right"));
add(new Label("Bottom left"));
add(new Label("Bottom right"));
setLayout(new TableLayout(3, "W")); // left aligned, by default
add(new Label("Left"));
add("FH", new Label("Filled horizontally"));
add("SE", new Label("Bottom right"));
setLayout(new TableLayout(2));
add("CS=2", new Label("Long label across the top"));
// bottom left is skipped
add("SKIP=1", new Label("Bottom right"));
TableLayout(int cols, String alignment, int hgap, int vgap); TableLayout(int cols, String alignment); // hgap=0, vgap=0 TableLayout(int cols); // alignment="C"
| cols | Number of columns, used when adding components to tell when to go to the next row |
| alignment | Default alignment for cells if not specified at the time of adding the component |
| hgap | Horizontal gap between cells and at edge (in pixels) |
| vgap | Vertical gap between cells and at edge (in pixels) |
| N,NE,E,SE, S,SW,W,NW | align to top, top right, etc. of cell |
| FH | fill horizontally (may be combined with N, C, S or FV to specify vertical behaviour) |
| FV | fill vertically (may be combined with E, C, W or FH to specify horizontal behaviour) |
| F | fill entire cell |
| C | centre component within cell |
| RS=n | span a given number of rows |
| CS=n | span a given number of columns |
| COL=n | force to a given column (where 0 is the first column, ie. start a new row early) |
| SKIP=n | skip a given number of columns |
| WT=n | specify a horizontal weighting factor, used to decide how much to expand the width of each column when there is extra space. Applies to this and subsequent rows. WT=-1 means use the preferred size of each column as the weighting factor; if all columns have weight -1 each column grows proportionately (the default). A weight of 0 means don't expand the width above its preferred size. |
Most flags can be combined together, eg. "RS=2,FH". If more than one alignment flag is specified they must be separated with commas. Case is significant, and spaces are not allowed.
Rolf Howarth
Last updated: 8 April 1998