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
FormAttachments
to optionally configure the left, top, right and bottom edges of each child. The following example code creates a FormLayout
and then sets it into a Shell
:
Display display = new Display (); Shell shell = new Shell(display); FormLayout layout = new FormLayout(); layout.marginWidth = 3; layout.marginHeight = 3; shell.setLayout(layout);
To use a FormLayout
, create a FormData
with FormAttachment
for each child of Composite
. The following example code attaches button1
to the top and left edge of the composite and button2
to the right edge of button1
and the top and right edges of the composite:
FormData data1 = new FormData(); data1.left = new FormAttachment(0, 0); data1.top = new FormAttachment(0, 0); button1.setLayoutData(data1); FormData data2 = new FormData(); data2.left = new FormAttachment(button1); data2.top = new FormAttachment(0, 0); data2.right = new FormAttachment(100, 0); button2.setLayoutData(data2);
Each side of a child control can be attached to a position in the parent composite, or to other controls within the Composite
by creating instances of FormAttachment
and setting them into the top, bottom, left, and right fields of the child's FormData
.
If a side is not given an attachment, it is defined as not being attached to anything, causing the child to remain at its preferred size. If a child is given no attachment on either the left or the right or top or bottom, it is automatically attached to the left and top of the composite respectively. The following code positions button1
and button2
but relies on default attachments:
FormData data2 = new FormData(); data2.left = new FormAttachment(button1); data2.right = new FormAttachment(100, 0); button2.setLayoutData(data2);
IMPORTANT: Do not define circular attachments. For example, do not attach the right edge of button1
to the left edge of button2
and then attach the left edge of button2
to the right edge of button1
. This will over constrain the layout, causing undefined behavior. The algorithm will terminate, but the results are undefined.
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 buildingbutton bars; 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 org.gwt.mosaic.ui.client.layout.builder.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} that ships with thesource 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 @author georgopoulos.georgios(at)gmail.com @see ColumnSpec @see RowSpec @see CellConstraints @see org.gwt.mosaic.ui.client.layout.builder.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|