|
TableLayoutConstraints
The preceeding examples have expressed the layout constraints in terms of a simple string. This is a
convenient short-hand notation, but it is not the only way to express constraints. The
TableLayoutConstraints class provides a means of manipulating constraints as objects. The following
three blocks of code are equivalent.
container.add (firstComponent, "1, 2, R, B");
container.add (secondComponent, "5, 8, 6, 10");
|
|
|
container.add (firstComponent,
new TableLayoutConstraints(1, 2, 1, 2, TableLayout.RIGHT, TableLayout.BOTTOM));
container.add (secondComponent,
new TableLayoutConstraints(5, 8, 6, 10, TableLayout.FULL, TableLayout.FULL));
|
|
|
TableLayoutConstraints c = new TableLayoutConstraints();
c.col1 = c.col2 = 1;
c.row1 = c.row2 = 2;
c.hAlign = TableLayout.RIGHT;
c.vAlign = TableLayout.BOTTOM;
container.add (firstComponent, c);
c.col1 = 5;
c.col2 = 6;
c.row1 = 8;
c.row2 = 10;
container.add (secondComponent, c);
|
|
There are two constructors for TableLayoutConstraints. One takes no parameters and defaults to the
short-hand "0, 0" or occupy a single cell at (0, 0) with full horizontal and vertical justification.
The second constructor takes six parameters: col1, row1, col2, row2, hAlign, and vAlign. The first
pair is the upper-left corner of the cell set. The second pair is the lower-right corner. If the
first and second pair match, the constraint refers to a single cell and the third pair is used for
justification.
Since the fields of TableLayoutConstraints are public, they may be manipulated directly. This was
done for convenience. The third example shows the fields being manipulated to set constraints.
Since TableLayout will make a copy of the TableLayoutConstraints, you can use the same constraints
object when setting the constraints for more than one component.
TableLayout has two methods pertaining to TableLayoutConstraints. getContstraints(Component)
returns the TableLayoutConstraints associated with the given component.
setConstraints(Component, TableLayoutConstraints) replaces the given component's
constraints with the newly supplied constraints. The next example will make use of these methods.
|