public final class LabelFieldLayout extends GridBagLayout
A simple example:
JPanel panel = new JPanel(); LabelFieldLayout l = new LabelFieldLayout(); panel.setLayout(l); panel.add(new JLabel("Label label:"), l.labelLabel()); panel.add(new JLabel("Label field"), l.labelField()); l.nextLine(); panel.add(new JLabel("Normal label:"), l.normalLabel()); panel.add(new JTextField("Normal field"), l.normalField()); l.nextLine(); panel.add(Box.createVerticalStrut(12), l.spacing()); l.nextLine(); panel.add(new JLabel("Wide label:"), l.wideLabel()); l.nextLine(); JList list = new JList(new Object[] { "Final wide field A", "Final wide field B", "Final wide field C" }); panel.add(new JScrollPane(list), l.finalWideField());
The labels are aligned in the left column and the fields are
aligned in the right column, except for a wide label or field
which stretches vertically across both columns (wideLabel()
or wideField()
). Rows containing fields that
are JLabels (labelLabel()
and labelField()
)
have less of a vertical gap than those containing other components
(normalLabel()
and normalField()
).
The final line in a LabelFieldLayout is the one which stretches vertically to cover space left at the bottom of a container. Use a final wide label
panel.add(new JLabel("Final wide label", l.finalWideLabel()));a final wide field
JList list = new JList(new Object[] {"Final wide field A", "Final wide field B", "Final wide field C" }); panel.add(new JScrollPane(list), l.finalWideField());or final spacing
panel.add(Box.createGlue(), l.finalSpacing());to allow the layout to adapt gracefully to container resize events.
The width of the label and field columns can be specified using
the constructor LabelFieldLayout(float, float)
or the
property setters setLabelPercent(float)
and
setFieldPercent(float)
. The default configuration is
to split on thirds, with 33% of the container width for labels and 66%
for fields.
Modifier and Type | Class and Description |
---|---|
static class |
LabelFieldLayout.Constraints
Constraints class that extends GridBagConstraints for
the single purpose of having the classname
LabelFieldLayout.Constraints . |
Modifier and Type | Field and Description |
---|---|
(package private) static float |
DEFAULT_FIELD_WIDTH
Default field width,
66% . |
(package private) static float |
DEFAULT_LABEL_WIDTH
Default label width,
33% . |
columnWeights, columnWidths, comptable, defaultConstraints, layoutInfo, MAXGRIDSIZE, MINSIZE, PREFERREDSIZE, rowHeights, rowWeights
Constructor and Description |
---|
LabelFieldLayout()
Create a new LabelFieldLayout in the default configuration,
split on thirds, with 33% of the container width for labels and 66%
for fields.
|
LabelFieldLayout(float labelPercent,
float fieldPercent)
Create a new LabelFieldLayout with the specified percentages
of the container width to use for labels and fields respectively.
|
Modifier and Type | Method and Description |
---|---|
LabelFieldLayout.Constraints |
field()
Return constraints suitable for a field.
|
LabelFieldLayout.Constraints |
finalSpacing()
Return constraints suitable for spacing that stretches
vertically to cover space left at the bottom of a container.
|
LabelFieldLayout.Constraints |
finalWideField()
Return constraints suitable for a final wide field, a field
that stretches horizontally across both the label and field
columns and stretches vertically to cover space left at the
bottom of a container.
|
LabelFieldLayout.Constraints |
finalWideLabel()
Return constraints suitable for a final wide label, a label
that stretches horizontally across both the label and field
columns and stretches vertically to cover space left at the
bottom of a container.
|
float |
getFieldPercent()
Return the percentage of the container width to
use for fields.
|
float |
getLabelPercent()
Return the percentage of the container width to
use for labels.
|
LabelFieldLayout.Constraints |
label()
Return constraints suitable for a label.
|
LabelFieldLayout.Constraints |
labelField()
Return constraints suitable for a label field, a field
that is a JLabel.
|
LabelFieldLayout.Constraints |
labelLabel()
Return constraints suitable for a label label, a label
for a field that is a JLabel.
|
(package private) LabelFieldLayout.Constraints |
macLabel()
Return constraints suitable for a mac label, a label
for a field that is not a JLabel.
|
(package private) LabelFieldLayout.Constraints |
macLabelLabel()
Return constraints suitable for a mac label label, a label
for a field that is a JLabel.
|
LabelFieldLayout |
nextLine()
Increment the layout cursor to the next line.
|
LabelFieldLayout.Constraints |
normalField()
Return constraints suitable for a normal field, a field
that is not a JLabel.
|
LabelFieldLayout.Constraints |
normalLabel()
Return constraints suitable for a normal label, a label
for a field that is not a JLabel.
|
void |
setFieldPercent(float fieldPercent)
Set the percentage of the container width to use
for fields to
fieldPercent . |
void |
setLabelPercent(float labelPercent)
Set the percentage of the container width to
use for labels to
labelPercent . |
LabelFieldLayout.Constraints |
spacing()
Return constraints suitable for spacing.
|
LabelFieldLayout.Constraints |
wideField()
Return constraints suitable for a wide field, a field
that stretches horizontally across both the label and field
columns.
|
LabelFieldLayout.Constraints |
wideLabel()
Return constraints suitable for a wide label, a label
that stretches horizontally across both the label and field
columns.
|
addLayoutComponent, addLayoutComponent, adjustForGravity, AdjustForGravity, arrangeGrid, ArrangeGrid, getConstraints, getLayoutAlignmentX, getLayoutAlignmentY, getLayoutDimensions, getLayoutInfo, GetLayoutInfo, getLayoutOrigin, getLayoutWeights, getMinSize, GetMinSize, invalidateLayout, layoutContainer, location, lookupConstraints, maximumLayoutSize, minimumLayoutSize, preferredLayoutSize, removeLayoutComponent, setConstraints, toString
static final float DEFAULT_LABEL_WIDTH
33%
.static final float DEFAULT_FIELD_WIDTH
66%
.public LabelFieldLayout()
public LabelFieldLayout(float labelPercent, float fieldPercent)
1.0f
.labelPercent
- the percentage of the container width
to use for labels, must be >= 0.0f
and <= 1.0f
fieldPercent
- the percentage of the container width
to use for fields, must be >= 0.0f
and <= 1.0f
public LabelFieldLayout nextLine()
public void setLabelPercent(float labelPercent)
labelPercent
. The
sum of the label and field percentages must be less
than 1.0f
.labelPercent
- the percentage of the container
width to use for labels, must be >= 0.0f
and <= 1.0f
public float getLabelPercent()
public void setFieldPercent(float fieldPercent)
fieldPercent
. The sum of
the label and field percentages must be less than or
equal to 1.0f
.fieldPercent
- the percentage of the container
width to use for fields, must be >= 0.0f
and <= 1.0f
public float getFieldPercent()
public LabelFieldLayout.Constraints label()
normalLabel()
public LabelFieldLayout.Constraints normalLabel()
LabelFieldLayout.Constraints macLabel()
public LabelFieldLayout.Constraints labelLabel()
LabelFieldLayout.Constraints macLabelLabel()
public LabelFieldLayout.Constraints wideLabel()
public LabelFieldLayout.Constraints finalWideLabel()
public LabelFieldLayout.Constraints field()
normalField()
public LabelFieldLayout.Constraints normalField()
public LabelFieldLayout.Constraints labelField()
public LabelFieldLayout.Constraints wideField()
public LabelFieldLayout.Constraints finalWideField()
public LabelFieldLayout.Constraints spacing()
public LabelFieldLayout.Constraints finalSpacing()
Copyright (c) 2003-2013 held jointly by the individual authors. Licensed under the GNU Lesser General Public License (LGPL).