:: JGOODIES :: Forms

:: Reference :: Cell Constraints ::

Each component managed by a FormLayout is associated with an instance of CellConstraints that specifies a component’s display area and alignment. The column and row origins are mandatory, but as we will see later, often a non-visual builder will automatically create the CellConstraints for you.

By default the column and row span is just 1, and the alignments are inherited from the related column and row. If possible you should specify the aligment for the column and row, not for the component; this way you can reduce the amount of alignment specifications significantly.

CellConstraints objects can be constructed in different ways using a mixture of ints, objects and strings. I recommend to specify the origin and span using ints and the alignment with strings - just to increase the code readability.

Reusing CellConstraints

You can reuse CellConstraints objects because they are cloned internally by the FormLayout. Hence you can write a condensed form
CellConstraints cc = new CellConstraints();
builder.addLabel("Name", cc.xy(1, 1));
builder.add(nameField,   cc.xy(3, 1));
builder.addLabel("Age",  cc.xy(1, 3));
builder.add(ageField,    cc.xy(3, 3));

Spanning Multiple Columns/Rows

You can let components span multiple columns or rows, for example by using the CellConstraints method #xywh where you specify the x and y position of the leading cell and the width and height of the display area.

Note: these components do not affect the size of the spanned columns or rows, nevertheless, they may expand the whole container. See also the FAQ for details and how to handle this situation.

Examples

1) Creation methods intended for use by humans
 CellConstraints cc = new CellConstraints();
 cc.xy(2, 1);                          // second col, first row
 cc.xy(2, 1, "right, bottom");         // aligned to right and bottom
 cc.xy(2, 1, "r, b");                  // abbreviated alignment
 cc.xywh(2, 1, 4, 3);                  // spans 4 cols, 3 rows
 cc.xywh(2, 1, 4, 3, "right, bottom");
 cc.xywh(2, 1, 4, 3, "r, b"); 
 
2) Constructors intended for builders
 new CellConstraints();                // first col, first row
 new CellConstraints(2, 1);			
 new CellConstraints(2, 1, 4, 3);
 new CellConstraints(2, 1, CellConstraints.RIGHT, 
                           CellConstraints.BOTTOM);	
3) Constructors intended for building UIs from XML
 CellConstraints cc = new CellConstraints();
 new CellConstraints("2, 1");
 new CellConstraints("2, 1, r, b");
 new CellConstraints("2, 1, 4, 3");
 new CellConstraints("2, 1, 4, 3, r, b");
 
(c) 2003 JGoodies