org.dishevelled.layout
Class LabelFieldLayout

java.lang.Object
  extended by java.awt.GridBagLayout
      extended by org.dishevelled.layout.LabelFieldLayout
All Implemented Interfaces:
LayoutManager, LayoutManager2, Serializable

public final class LabelFieldLayout
extends GridBagLayout

A wrapper around GridBagLayout that simplifies things for the common case of two columns, one of labels and one of fields.

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.

Version:
$Revision: 741 $ $Date: 2010-01-02 20:23:15 -0600 (Sat, 02 Jan 2010) $
Author:
Michael Heuer
See Also:
Serialized Form

Nested Class Summary
static class LabelFieldLayout.Constraints
          Constraints class that extends GridBagConstraints for the single purpose of having the classname LabelFieldLayout.Constraints.
 
Field Summary
(package private) static float DEFAULT_FIELD_WIDTH
          Default field width, 66%.
(package private) static float DEFAULT_LABEL_WIDTH
          Default label width, 33%.
 
Fields inherited from class java.awt.GridBagLayout
columnWeights, columnWidths, comptable, defaultConstraints, layoutInfo, MAXGRIDSIZE, MINSIZE, PREFERREDSIZE, rowHeights, rowWeights
 
Constructor Summary
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.
 
Method Summary
 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.
 
Methods inherited from class java.awt.GridBagLayout
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
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

DEFAULT_LABEL_WIDTH

static final float DEFAULT_LABEL_WIDTH
Default label width, 33%.

See Also:
Constant Field Values

DEFAULT_FIELD_WIDTH

static final float DEFAULT_FIELD_WIDTH
Default field width, 66%.

See Also:
Constant Field Values
Constructor Detail

LabelFieldLayout

public 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

public LabelFieldLayout(float labelPercent,
                        float fieldPercent)
Create a new LabelFieldLayout with the specified percentages of the container width to use for labels and fields respectively. The sum of the label and field percentages must be less than or equal to 1.0f.

Parameters:
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
Method Detail

nextLine

public LabelFieldLayout nextLine()
Increment the layout cursor to the next line. Returns this instance of LabelFieldLayout, for use in chaining calls.

Returns:
this instance of LabelFieldLayout, for use in chaining method calls

setLabelPercent

public void setLabelPercent(float labelPercent)
Set the percentage of the container width to use for labels to labelPercent. The sum of the label and field percentages must be less than 1.0f.

Parameters:
labelPercent - the percentage of the container width to use for labels, must be >= 0.0f and <= 1.0f

getLabelPercent

public float getLabelPercent()
Return the percentage of the container width to use for labels.

Returns:
the percentage of the container width to use for labels

setFieldPercent

public void setFieldPercent(float fieldPercent)
Set the percentage of the container width to use for fields to fieldPercent. The sum of the label and field percentages must be less than or equal to 1.0f.

Parameters:
fieldPercent - the percentage of the container width to use for fields, must be >= 0.0f and <= 1.0f

getFieldPercent

public float getFieldPercent()
Return the percentage of the container width to use for fields.

Returns:
the percentage of the container width to use for fields

label

public LabelFieldLayout.Constraints label()
Return constraints suitable for a label.

Returns:
constraints suitable for a label.
See Also:
normalLabel()

normalLabel

public LabelFieldLayout.Constraints normalLabel()
Return constraints suitable for a normal label, a label for a field that is not a JLabel.

Returns:
constraints suitable for a normal label

macLabel

LabelFieldLayout.Constraints macLabel()
Return constraints suitable for a mac label, a label for a field that is not a JLabel.

Returns:
constraints suitable for a mac label

labelLabel

public LabelFieldLayout.Constraints labelLabel()
Return constraints suitable for a label label, a label for a field that is a JLabel.

Returns:
constraints suitable for a label label

macLabelLabel

LabelFieldLayout.Constraints macLabelLabel()
Return constraints suitable for a mac label label, a label for a field that is a JLabel.

Returns:
constraints suitable for a mac label label

wideLabel

public LabelFieldLayout.Constraints wideLabel()
Return constraints suitable for a wide label, a label that stretches horizontally across both the label and field columns.

Returns:
constraints suitable for a wide label

finalWideLabel

public 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.

Returns:
constraints suitable for a final wide label

field

public LabelFieldLayout.Constraints field()
Return constraints suitable for a field.

Returns:
constraints suitable for a field
See Also:
normalField()

normalField

public LabelFieldLayout.Constraints normalField()
Return constraints suitable for a normal field, a field that is not a JLabel.

Returns:
constraints suitable for a normal field

labelField

public LabelFieldLayout.Constraints labelField()
Return constraints suitable for a label field, a field that is a JLabel.

Returns:
constraints suitable for a label field

wideField

public LabelFieldLayout.Constraints wideField()
Return constraints suitable for a wide field, a field that stretches horizontally across both the label and field columns.

Returns:
constraints suitable for a wide field

finalWideField

public 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.

Returns:
constraints suitable for a final wide field

spacing

public LabelFieldLayout.Constraints spacing()
Return constraints suitable for spacing.

Returns:
constraints suitable for spacing

finalSpacing

public LabelFieldLayout.Constraints finalSpacing()
Return constraints suitable for spacing that stretches vertically to cover space left at the bottom of a container.

Returns:
constraints suitable for spacing that stretches vertically to cover space left at the bottom of a container


Copyright (c) 2003-2010 held jointly by the individual authors. Licensed under the GNU Lesser General Public License (LGPL).