CookFormLayout Tutorial: DefaultFormBuilder


 

 

Support This Project

 

stats counter

 

Logo SourceForge.net Logo IntelliJ IDEA

Tutorial Example

tutorial_formbuilder.xml in the CookFormLayout Java Web Start demo.

Introduction

In previous two tutorials, one problem was persistent. One always had to specify the CellConstraints even when we were adding components in order. JGoodies DefaultFormBuilder (supported via <formbuilder>) solves the problem by using an internal cursor that keeps track of the current location to add the new component.

In this tutorial, you will see that all CellConstraints specifications are avoided. Instead, we use tags <form-nextline> and <form-nextcolumn> to move the cursor location. Also, columnspan and nextline attributes of <form-label> can be used to advance the cursor and adjust the new component size. In most cases, we can also simply add a single cell component directly to the <formbuilder>.

1. Specify DefaultFormBuilder

To use the PanelBuilder, first place <formbuilder> inside a <panel> tag, and then place a <formlayout> tag inside the <formbuilder>.

<panel>
	<formbuilder>
		<formlayout
			columnspec="pref, 4dlu, 50dlu, 4dlu, min"
			rowspec="pref, 2dlu, pref, 2dlu, pref, 2dlu, pref"
		>
			<form-rowgroup rows="3, 5, 7"/>
		</formlayout>
	</formbuilder>
</panel>

It is very important to have the <formlayout specified or errors would occur.

2. Add Swing Components

With <formbuilder, it is straightfoward to add Swing components that only takes a single cell.

		<label text="Label 3"/>
		<textfield/>
		<button text="..."/>

For associated label and components using <form-label>

		<form-label text="Label 1" columnspan="3" nextline="true">
			<textfield/>
		</form-label>

Note that, to add a Swing component (w/o label) that spans several columns, one still uses <form-label> to do the job, just without the text attribute specified.

3. Control Internal Layout Cursor Location

Use <form-nextline> and <form-nextcolumn> to advance the internal cursor location. <form-label> also has the ability to end the current row with nextline="true" attribute setting.

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

Valid XHTML 1.0! Valid CSS!