goodies.com/articles/forms.pdf">online.
To use FormLayout you first define the grid by specifying the columns and rows. In a second step you add components to the grid. You can specify columns and rows via human-readable String descriptions or via arrays of {@link ColumnSpec} and {@link RowSpec} instances.
Each component managed by a FormLayout is associated with an instance of {@link CellConstraints}. The constraints object specifies where a component should be located on the form's grid and how the component should be positioned. In addition to its constraints object the FormLayout
also considers each component's minimum and preferred sizes in order to determine a component's size.
FormLayout has been designed to work with non-visual builders that help you specify the layout and fill the grid. For example, the {@link com.jgoodies.forms.builder.ButtonBarBuilder2} assists you in building buttonbars; it creates a standardized FormLayout and provides a minimal API that specializes in adding buttons and Actions. Other builders can create frequently used panel design, for example a form that consists of rows of label-component pairs.
FormLayout has been prepared to work with different types of sizes as defined by the {@link Size} interface.
Example 1 (Plain FormLayout):
The following example creates a panel with 3 data columns and 3 data rows; the columns and rows are specified before components are added to the form.
FormLayout layout = new FormLayout( "right:pref, 6dlu, 50dlu, 4dlu, default", // columns "pref, 3dlu, pref, 3dlu, pref"); // rows CellConstraints cc = new CellConstraints(); JPanel panel = new JPanel(layout); panel.add(new JLabel("Label1"), cc.xy (1, 1)); panel.add(new JTextField(), cc.xywh(3, 1, 3, 1)); panel.add(new JLabel("Label2"), cc.xy (1, 3)); panel.add(new JTextField(), cc.xy (3, 3)); panel.add(new JLabel("Label3"), cc.xy (1, 5)); panel.add(new JTextField(), cc.xy (3, 5)); panel.add(new JButton("/u2026"), cc.xy (5, 5)); return panel;
Example 2 (Using PanelBuilder):
This example creates the same panel as above using the {@link com.jgoodies.forms.builder.PanelBuilder} to add components to the form.
FormLayout layout = new FormLayout( "right:pref, 6dlu, 50dlu, 4dlu, default", // columns "pref, 3dlu, pref, 3dlu, pref"); // rows PanelBuilder builder = new PanelBuilder(layout); CellConstraints cc = new CellConstraints(); builder.addLabel("Label1", cc.xy (1, 1)); builder.add(new JTextField(), cc.xywh(3, 1, 3, 1)); builder.addLabel("Label2", cc.xy (1, 3)); builder.add(new JTextField(), cc.xy (3, 3)); builder.addLabel("Label3", cc.xy (1, 5)); builder.add(new JTextField(), cc.xy (3, 5)); builder.add(new JButton("/u2026"), cc.xy (5, 5)); return builder.getPanel();
Example 3 (Using DefaultFormBuilder):
This example utilizes the {@link com.jgoodies.forms.builder.DefaultFormBuilder} thatships with the source distribution.
FormLayout layout = new FormLayout( "right:pref, 6dlu, 50dlu, 4dlu, default"); // 5 columns; add rows later DefaultFormBuilder builder = new DefaultFormBuilder(layout); builder.append("Label1", new JTextField(), 3); builder.append("Label2", new JTextField()); builder.append("Label3", new JTextField()); builder.append(new JButton("/u2026")); return builder.getPanel();
@author Karsten Lentzsch
@version $Revision: 1.19 $
@see ColumnSpec
@see RowSpec
@see CellConstraints
@see com.jgoodies.forms.builder.AbstractFormBuilder
@see com.jgoodies.forms.builder.ButtonBarBuilder
@see com.jgoodies.forms.builder.DefaultFormBuilder
@see com.jgoodies.forms.factories.FormFactory
@see Size
@see Sizes