public final class DefaultFormBuilder extends I15dPanelBuilder
FormLayout
. This builder combines frequently used
panel building steps: add a new row, add a label, proceed to the next
data column, then add a component.
The extra value lies in the #append
methods that
append gap rows and component rows if necessary and then add
the given components. They are built upon the superclass behavior
#appendRow
and the set of #add
methods.
A set of component appenders allows to add a textual label and
associated component in a single step.
This builder can map resource keys to internationalized (i15d) texts
when creating text labels, titles and titled separators. Therefore
you must specify a ResourceBundle
in the constructor.
The builder methods throw an IllegalStateException
if one
of the mapping builder methods is invoked and no bundle has been set.
You can configure the build process by setting a leading column, enabling the row grouping and by modifying the gaps between normal lines and between paragraphs. The leading column will be honored if the cursor proceeds to the next row. All appended components start in the specified lead column, except appended separators that span all columns.
It is tempting to use the DefaultFormBuilder all the time and to let it add rows automatically. Use a simpler style if it increases the code readability. Explicit row specifications and cell constraints make your layout easier to understand - but harder to maintain. See also the accompanying tutorial sources and the Tips & Tricks that are part of the Forms documentation.
Sometimes a form consists of many standardized rows but has a few
rows that require a customization. The DefaultFormBuilder can do everything
that the superclasses AbstractFormBuilder
and PanelBuilder
can do;
among other things: appending new rows and moving the cursor.
Again, ask yourself if the DefaultFormBuilder is the appropriate builder.
As a rule of thumb you should have more components than builder commands.
There are different ways to add custom rows. Find below example code
that presents and compares the pros and cons of three approaches.
The texts used in methods #append(String, ...)
and
#appendTitle(String)
as well as the localized texts used in
methods #appendI15d
and #appendI15dTitle
can contain an optional mnemonic marker. The mnemonic and mnemonic index
are indicated by a single ampersand (&).
For example "&Save", or
"Save &as". To use the ampersand itself,
duplicate it, for example "Look&&Feel".
Example:
public void build() { FormLayout layout = new FormLayout( "right:max(40dlu;pref), 3dlu, 80dlu, 7dlu, " // 1st major column + "right:max(40dlu;pref), 3dlu, 80dlu", // 2nd major column ""); // add rows dynamically DefaultFormBuilder builder = new DefaultFormBuilder(layout); builder.setDefaultDialogBorder(); builder.appendSeparator("Flange"); builder.append("Identifier", identifierField); builder.nextLine(); builder.append("PTI [kW]", new JTextField()); builder.append("Power [kW]", new JTextField()); builder.append("s [mm]", new JTextField()); builder.nextLine(); builder.appendSeparator("Diameters"); builder.append("da [mm]", new JTextField()); builder.append("di [mm]", new JTextField()); builder.append("da2 [mm]", new JTextField()); builder.append("di2 [mm]", new JTextField()); builder.append("R [mm]", new JTextField()); builder.append("D [mm]", new JTextField()); builder.appendSeparator("Criteria"); builder.append("Location", buildLocationComboBox()); builder.append("k-factor", new JTextField()); builder.appendSeparator("Bolts"); builder.append("Material", ViewerUIFactory.buildMaterialComboBox()); builder.nextLine(); builder.append("Numbers", new JTextField()); builder.nextLine(); builder.append("ds [mm]", new JTextField()); }
Custom Row Example:
public JComponent buildPanel() { initComponents(); FormLayout layout = new FormLayout( "right:pref, 3dlu, default:grow", ""); DefaultFormBuilder builder = new DefaultFormBuilder(layout); builder.setDefaultDialogBorder(); builder.setRowGroupingEnabled(true); CellConstraints cc = new CellConstraints(); // In this approach, we add a gap and a custom row. // The advantage of this approach is, that we can express // the row spec and comment area cell constraints freely. // The disadvantage is the misalignment of the leading label. // Also the row's height may be inconsistent with other rows. builder.appendSeparator("Single Custom Row"); builder.append("Name", name1Field); builder.appendRow(builder.getLineGapSpec()); builder.appendRow(new RowSpec("top:31dlu")); // Assumes line is 14, gap is 3 builder.nextLine(2); builder.append("Comment"); builder.add(new JScrollPane(comment1Area), cc.xy(builder.getColumn(), builder.getRow(), "fill, fill")); builder.nextLine(); // In this approach, we append a standard row with gap before it. // The advantage is, that the leading label is aligned well. // The disadvantage is that the comment area now spans // multiple cells and is slightly less flexible. // Also the row's height may be inconsistent with other rows. builder.appendSeparator("Standard + Custom Row"); builder.append("Name", name2Field); builder.append("Comment"); builder.appendRow(new RowSpec("17dlu")); // Assumes line is 14, gap is 3 builder.add(new JScrollPane(comment2Area), cc.xywh(builder.getColumn(), builder.getRow(), 1, 2)); builder.nextLine(2); // In this approach, we append two standard rows with associated gaps. // The advantage is, that the leading label is aligned well, // and the height is consistent with other rows. // The disadvantage is that the comment area now spans // multiple cells and is slightly less flexible. builder.appendSeparator("Two Standard Rows"); builder.append("Name", name3Field); builder.append("Comment"); builder.nextLine(); builder.append(""); builder.nextRow(-2); builder.add(new JScrollPane(comment3Area), cc.xywh(builder.getColumn(), builder.getRow(), 1, 3)); return builder.getPanel(); }
TODO: Consider adding a method for appending a component that spans the
remaining columns in the current row. Method name candidates are
#appendFullSpan
and #appendRemaining
.
AbstractFormBuilder
,
FormFactory
,
FormLayout
Constructor and Description |
---|
DefaultFormBuilder(FormLayout layout)
Constructs an instance of
DefaultFormBuilder for the given
layout. |
DefaultFormBuilder(FormLayout layout,
javax.swing.JPanel panel)
Constructs an instance of
DefaultFormBuilder for the given
panel and layout. |
DefaultFormBuilder(FormLayout layout,
java.util.ResourceBundle bundle)
Constructs an instance of
DefaultFormBuilder for the given
layout and resource bundle. |
DefaultFormBuilder(FormLayout layout,
java.util.ResourceBundle bundle,
javax.swing.JPanel panel)
Constructs an instance of
DefaultFormBuilder for the given
panel, layout and resource bundle. |
DefaultFormBuilder(javax.swing.JPanel panel,
FormLayout layout)
Deprecated.
Replaced by
DefaultFormBuilder(FormLayout, JPanel) . |
DefaultFormBuilder(javax.swing.JPanel panel,
FormLayout layout,
java.util.ResourceBundle bundle)
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
void |
append(java.awt.Component component)
Adds a component to the panel using the default constraints
with a column span of 1.
|
void |
append(java.awt.Component c1,
java.awt.Component c2)
Adds two components to the panel; each component will span a single
data column.
|
void |
append(java.awt.Component c1,
java.awt.Component c2,
java.awt.Component c3)
Adds three components to the panel; each component will span a single
data column.
|
void |
append(java.awt.Component component,
int columnSpan)
Adds a component to the panel using the default constraints with
the given columnSpan.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic)
Adds a text label to the panel and proceeds to the next column.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic,
java.awt.Component component)
Adds a text label and component to the panel.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic,
java.awt.Component c,
boolean nextLine)
Adds a text label and component to the panel; the component will span
the specified number columns.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic,
java.awt.Component c1,
java.awt.Component c2)
Adds a text label and two components to the panel; each component
will span a single column.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic,
java.awt.Component c1,
java.awt.Component c2,
java.awt.Component c3)
Adds a text label and three components to the panel; each component
will span a single column.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic,
java.awt.Component c1,
java.awt.Component c2,
java.awt.Component c3,
java.awt.Component c4)
Adds a text label and four components to the panel; each component
will span a single column.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic,
java.awt.Component c1,
java.awt.Component c2,
int colSpan)
Adds a text label and two components to the panel; each component
will span a single column.
|
javax.swing.JLabel |
append(java.lang.String textWithMnemonic,
java.awt.Component c,
int columnSpan)
Adds a text label and component to the panel; the component will span
the specified number columns.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey)
Adds an internationalized (i15d) text label to the panel using
the given resource key and proceeds to the next column.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey,
java.awt.Component component)
Adds an internationalized (i15d) text label and component
to the panel.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey,
java.awt.Component component,
boolean nextLine)
Adds an internationalized (i15d) text label and component
to the panel.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey,
java.awt.Component c1,
java.awt.Component c2)
Adds an internationalized (i15d) text label and two components
to the panel; each component will span a single column.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey,
java.awt.Component c1,
java.awt.Component c2,
java.awt.Component c3)
Adds an internationalized (i15d) text label and three components
to the panel; each component will span a single column.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey,
java.awt.Component c1,
java.awt.Component c2,
java.awt.Component c3,
java.awt.Component c4)
Adds an internationalized (i15d) text label and four components
to the panel; each component will span a single column.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey,
java.awt.Component c1,
java.awt.Component c2,
int colSpan)
Adds an internationalized (i15d) text label and two components
to the panel; each component will span a single column.
|
javax.swing.JLabel |
appendI15d(java.lang.String resourceKey,
java.awt.Component c,
int columnSpan)
Adds an internationalized (i15d) text label to the panel using
the given resource key; then proceeds to the next data column
and adds a component with the given column span.
|
javax.swing.JComponent |
appendI15dSeparator(java.lang.String resourceKey)
Appends an internationalized titled separator for
the given resource key that spans all columns.
|
javax.swing.JLabel |
appendI15dTitle(java.lang.String resourceKey)
Adds an internationalized title label to the panel and
proceeds to the next column.
|
javax.swing.JComponent |
appendSeparator()
Adds a separator without text that spans all columns.
|
javax.swing.JComponent |
appendSeparator(java.lang.String text)
Adds a separator with the given text that spans all columns.
|
javax.swing.JLabel |
appendTitle(java.lang.String textWithMnemonic)
Adds a title label to the panel and proceeds to the next column.
|
protected int |
getLeadingColumn()
Returns the leading column.
|
int |
getLeadingColumnOffset()
Returns the offset of the leading column, often 0 or 1.
|
RowSpec |
getLineGapSpec()
Returns the row specification that is used to separate component lines.
|
boolean |
isRowGroupingEnabled()
Returns whether new data rows are being grouped or not.
|
void |
setLeadingColumnOffset(int columnOffset)
Sets the offset of the leading column, often 0 or 1.
|
void |
setLineGapSize(ConstantSize lineGapSize)
Sets the size of gaps between component lines using the given
constant size.
|
void |
setParagraphGapSize(ConstantSize paragraphGapSize)
Sets the size of gaps between paragraphs using the given
constant size.
|
void |
setRowGroupingEnabled(boolean enabled)
Enables or disables the grouping of new data rows.
|
add15dTitle, addI15dLabel, addI15dLabel, addI15dLabel, addI15dSeparator, addI15dSeparator, addI15dTitle, getI15dString
add, addLabel, addLabel, addLabel, addLabel, addSeparator, addSeparator, addSeparator, addSeparator, addTitle, addTitle, addTitle, getComponentFactory, getPanel, setBorder, setComponentFactory, setDefaultDialogBorder
add, add, add, appendColumn, appendColumn, appendGlueColumn, appendGlueRow, appendLabelComponentsGapColumn, appendParagraphGapRow, appendRelatedComponentsGapColumn, appendRelatedComponentsGapRow, appendRow, appendRow, appendUnrelatedComponentsGapColumn, appendUnrelatedComponentsGapRow, cellConstraints, createLeftAdjustedConstraints, getColumn, getColumnCount, getColumnIncrementSign, getContainer, getLayout, getRow, getRowCount, isLeftToRight, nextColumn, nextColumn, nextLine, nextLine, nextRow, nextRow, setAlignment, setBounds, setColumn, setColumnSpan, setExtent, setHAlignment, setLeftToRight, setOrigin, setRow, setRowSpan, setVAlignment
public DefaultFormBuilder(FormLayout layout)
DefaultFormBuilder
for the given
layout.layout
- the FormLayout
to be usedpublic DefaultFormBuilder(FormLayout layout, javax.swing.JPanel panel)
DefaultFormBuilder
for the given
panel and layout.layout
- the FormLayout
to be usedpanel
- the layout containerpublic DefaultFormBuilder(FormLayout layout, java.util.ResourceBundle bundle)
DefaultFormBuilder
for the given
layout and resource bundle.layout
- the FormLayout
to be usedbundle
- the ResourceBundle
used to lookup i15d
stringspublic DefaultFormBuilder(FormLayout layout, java.util.ResourceBundle bundle, javax.swing.JPanel panel)
DefaultFormBuilder
for the given
panel, layout and resource bundle.layout
- the FormLayout
to be usedpanel
- the layout containerbundle
- the ResourceBundle
used to lookup i15d
strings@Deprecated public DefaultFormBuilder(javax.swing.JPanel panel, FormLayout layout)
DefaultFormBuilder(FormLayout, JPanel)
.DefaultFormBuilder
for the given
panel and layout.panel
- the layout containerlayout
- the FormLayout
to be used@Deprecated public DefaultFormBuilder(javax.swing.JPanel panel, FormLayout layout, java.util.ResourceBundle bundle)
DefaultFormBuilder(FormLayout, ResourceBundle, JPanel)
.DefaultFormBuilder
for the given
panel, layout and resource bundle.panel
- the layout containerlayout
- the FormLayout
to be usedbundle
- the ResourceBundle
used to lookup i15d
stringspublic RowSpec getLineGapSpec()
RowSpec
that is used to separate linespublic void setLineGapSize(ConstantSize lineGapSize)
Examples:
builder.setLineGapSize(Sizes.ZERO); builder.setLineGapSize(Sizes.DLUY9); builder.setLineGapSize(Sizes.pixel(1));
lineGapSize
- the ConstantSize
that describes
the size of the gaps between component linespublic void setParagraphGapSize(ConstantSize paragraphGapSize)
Examples:
builder.setParagraphGapSize(Sizes.DLUY14); builder.setParagraphGapSize(Sizes.dluY(22)); builder.setParagraphGapSize(Sizes.pixel(42));
paragraphGapSize
- the ConstantSize
that describes
the size of the gaps between paragraphspublic int getLeadingColumnOffset()
public void setLeadingColumnOffset(int columnOffset)
columnOffset
- the new offset of the leading columnpublic boolean isRowGroupingEnabled()
public void setRowGroupingEnabled(boolean enabled)
enabled
- indicates grouping enabled, false disabledpublic void append(java.awt.Component component)
component
- the component to addpublic void append(java.awt.Component component, int columnSpan)
component
- the component to appendcolumnSpan
- the column span used to addpublic void append(java.awt.Component c1, java.awt.Component c2)
c1
- the first component to addc2
- the second component to addpublic void append(java.awt.Component c1, java.awt.Component c2, java.awt.Component c3)
c1
- the first component to addc2
- the second component to addc3
- the third component to addpublic javax.swing.JLabel append(java.lang.String textWithMnemonic)
textWithMnemonic
- the label's text - may mark a mnemonicpublic javax.swing.JLabel append(java.lang.String textWithMnemonic, java.awt.Component component)
The created label is labelling the given component; so the component gets the focus if the (optional) label mnemonic is pressed.
textWithMnemonic
- the label's text - may mark a mnemoniccomponent
- the component to addpublic javax.swing.JLabel append(java.lang.String textWithMnemonic, java.awt.Component c, boolean nextLine)
The created label is labelling the given component; so the component gets the focus if the (optional) label mnemonic is pressed.
textWithMnemonic
- the label's text - may mark a mnemonicc
- the component to addnextLine
- true forces a next lineJLabel.setLabelFor(java.awt.Component)
public javax.swing.JLabel append(java.lang.String textWithMnemonic, java.awt.Component c, int columnSpan)
The created label is labelling the given component; so the component gets the focus if the (optional) label mnemonic is pressed.
textWithMnemonic
- the label's text - may mark a mnemonicc
- the component to addcolumnSpan
- number of columns the component shall spanJLabel.setLabelFor(java.awt.Component)
public javax.swing.JLabel append(java.lang.String textWithMnemonic, java.awt.Component c1, java.awt.Component c2)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
textWithMnemonic
- the label's text - may mark a mnemonicc1
- the first component to addc2
- the second component to addpublic javax.swing.JLabel append(java.lang.String textWithMnemonic, java.awt.Component c1, java.awt.Component c2, int colSpan)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
textWithMnemonic
- the label's text - may mark a mnemonicc1
- the first component to addc2
- the second component to addcolSpan
- the column span for the second componentpublic javax.swing.JLabel append(java.lang.String textWithMnemonic, java.awt.Component c1, java.awt.Component c2, java.awt.Component c3)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
textWithMnemonic
- the label's text - may mark a mnemonicc1
- the first component to addc2
- the second component to addc3
- the third component to addpublic javax.swing.JLabel append(java.lang.String textWithMnemonic, java.awt.Component c1, java.awt.Component c2, java.awt.Component c3, java.awt.Component c4)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
textWithMnemonic
- the label's text - may mark a mnemonicc1
- the first component to addc2
- the second component to addc3
- the third component to addc4
- the fourth component to addpublic javax.swing.JLabel appendI15d(java.lang.String resourceKey)
resourceKey
- the resource key for the the label's textpublic javax.swing.JLabel appendI15d(java.lang.String resourceKey, java.awt.Component component)
The created label is labelling the given component; so the component gets the focus if the (optional) label mnemonic is pressed.
resourceKey
- the resource key for the text to addcomponent
- the component to addpublic javax.swing.JLabel appendI15d(java.lang.String resourceKey, java.awt.Component component, boolean nextLine)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
resourceKey
- the resource key for the text to addcomponent
- the component to addnextLine
- true forces a next linepublic javax.swing.JLabel appendI15d(java.lang.String resourceKey, java.awt.Component c, int columnSpan)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
resourceKey
- the resource key for the text to addc
- the component to addcolumnSpan
- number of columns the component shall spanpublic javax.swing.JLabel appendI15d(java.lang.String resourceKey, java.awt.Component c1, java.awt.Component c2)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
resourceKey
- the resource key for the text to addc1
- the first component to addc2
- the second component to addpublic javax.swing.JLabel appendI15d(java.lang.String resourceKey, java.awt.Component c1, java.awt.Component c2, int colSpan)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
resourceKey
- the resource key for the text to addc1
- the first component to addc2
- the second component to addcolSpan
- the column span for the second componentpublic javax.swing.JLabel appendI15d(java.lang.String resourceKey, java.awt.Component c1, java.awt.Component c2, java.awt.Component c3)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
resourceKey
- the resource key for the text to addc1
- the first component to addc2
- the second component to addc3
- the third component to addpublic javax.swing.JLabel appendI15d(java.lang.String resourceKey, java.awt.Component c1, java.awt.Component c2, java.awt.Component c3, java.awt.Component c4)
The created label is labelling the first component; so the component gets the focus if the (optional) label mnemonic is pressed.
resourceKey
- the resource key for the text to addc1
- the first component to addc2
- the second component to addc3
- the third component to addc4
- the third component to addpublic javax.swing.JLabel appendTitle(java.lang.String textWithMnemonic)
textWithMnemonic
- the label's text - may mark a mnemonicpublic javax.swing.JLabel appendI15dTitle(java.lang.String resourceKey)
resourceKey
- the resource key for the title's textpublic javax.swing.JComponent appendSeparator()
public javax.swing.JComponent appendSeparator(java.lang.String text)
text
- the separator title textpublic javax.swing.JComponent appendI15dSeparator(java.lang.String resourceKey)
resourceKey
- the resource key for the separator title's textprotected int getLeadingColumn()
getLeadingColumn
in class AbstractFormBuilder