CookFormLayout Tutorial: FormLayout



Support This Project


stats counter


Logo Logo IntelliJ IDEA

Tutorial Example

tutorial_formlayout.xml in the CookFormLayout Java Web Start demo.

1. Install CookFormLayout Tags

To install CookFormLayout tags to CookSwing, use the Java code below. It only needs to be called once per application.

	CookFormLayout.setupTagLibrary (CookSwing.getSwingTagLibrary ())

2. Specify FormLayout

To use the FormLayout, first place <formlayout> inside a <panel> tag.

		columnspec="pref, 4dlu, 50dlu, 4dlu, min"
		rowspec="pref, 2dlu, pref, 2dlu, pref"

FormLayout uses a grid like layout. The beauty of this layout manager, however, lies in the simple yet powerful specifications of how grids are constructed.

JGoodies FormLayout can also let developers change a particular row/column spec in the middle of adding components. However, such feature is not in <formlayout> since <formbuilder> offers such ability. I may add this feature in later one if it is really necessary :)

3. Add Swing Components with CellConstraints

Then to add a Swing component to this layout, put the component inside a <cellconstraints> tag, which is in turn inside <formlayout>.

		<cellconstraints value="1, 1">
			<label text="Label1"/>

There are a number of ways of specifying CellConstraints. For people who have experiences doing FormLayout, the easiest way is to use the "value" attribute to specify all the constraints, like:

		<cellconstraints value="1, 1">

Or, if you prefer, you can be slightly more clear with

		<cellconstraints xy="1, 1">

Or, if you are serious about being very clear

		<cellconstraints gridx="1" gridy="1">

Refer to the <cellconstraints> tag reference for a complete list of attributes available.

Note that in FormLayout, the index values of the x/y grid starts from 1, not zero.

4. Row and Column Groups

Row group is useful to set the height of several rows to the same. <form-rowgroup> is used to accomplish the task. In our case, only one row group is available. <form-columngroup> is used for the equivalent setting for the column.

		<form-rowgroup rows="3, 5, 7"/>

5. Separator

<form-separator> tag provides a convenient way of generating a Separator text using FormLayout's ComponentFactory.

		<form-separator text="<formlayout> Tutorial" cellconstraints="1, 1, 5, 1"/>

(c) Copyright 2004-2007 Heng Yuan. All rights reserved.

Valid XHTML 1.0! Valid CSS!