A non-visual builder that assists you in building consistent button bars that
comply with popular UI style guides. It utilizes the
FormLayout .
This class is in turn used by the
com.jgoodies.forms.factories.ButtonBarFactory that provides an even
higher level of abstraction for building consistent button bars.
Buttons added to the builder are either gridded or fixed and may fill their
FormLayout cell or not. All gridded buttons get the same width, while fixed
buttons use their own size. Gridded buttons honor the default minimum button
width as specified by the current
com.jgoodies.forms.util.LayoutStyle .
You can set an optional hint for narrow margin for the fixed width buttons.
This is useful if you want to lay out a button bar that includes a button
with a long text. For example, in a bar with 'Copy to Clipboard', 'OK',
'Cancel' you may declare the clipboard button as a fixed size button with
narrow margins, OK and Cancel as gridded. Gridded buttons are marked as
narrow by default. Note that some look&feels do not support the narrow
margin feature, and conversely, others have only narrow margins. The JGoodies
look&feels honor the setting, the Mac Aqua l&f uses narrow margins
all the time.
To honor the platform's button order (left-to-right vs. right-to-left) this
builder uses the leftToRightButtonOrder property. It is
initialized with the current LayoutStyle's button order, which in turn is
left-to-right on most platforms and right-to-left on the Mac OS X. Builder
methods that create sequences of buttons (e.g.
ButtonBarBuilder.addGriddedButtons(JButton[]) honor the button order. If you want to
ignore the default button order, you can either add add individual buttons,
or create a ButtonBarBuilder instance with the order set to left-to-right.
For the latter see
ButtonBarBuilder.createLeftToRightBuilder() . Also see the button
order example below.
Example:
The following example builds a button bar with Help button on the
left-hand side and OK, Cancel, Apply buttons on the right-hand side.
private JPanel createHelpOKCancelApplyBar(JButton help, JButton ok, JButton cancel, JButton apply) {
ButtonBarBuilder builder = new ButtonBarBuilder();
builder.addGridded(help);
builder.addRelatedGap();
builder.addGlue();
builder.addGriddedButtons(new JButton[] { ok, cancel, apply });
return builder.getPanel();
}
Button Order Example:
The following example builds three button bars where one honors the
platform's button order and the other two ignore it.
public JComponent buildPanel() {
FormLayout layout = new FormLayout("pref");
DefaultFormBuilder rowBuilder = new DefaultFormBuilder(layout);
rowBuilder.setDefaultDialogBorder();
rowBuilder.append(buildButtonSequence(new ButtonBarBuilder()));
rowBuilder.append(buildButtonSequence(ButtonBarBuilder.createLeftToRightBuilder()));
rowBuilder.append(buildIndividualButtons(new ButtonBarBuilder()));
return rowBuilder.getPanel();
}
private Component buildButtonSequence(ButtonBarBuilder builder) {
builder.addGriddedButtons(new JButton[] { new JButton("One"), new JButton("Two"), new JButton("Three") });
return builder.getPanel();
}
private Component buildIndividualButtons(ButtonBarBuilder builder) {
builder.addGridded(new JButton("One"));
builder.addRelatedGap();
builder.addGridded(new JButton("Two"));
builder.addRelatedGap();
builder.addGridded(new JButton("Three"));
return builder.getPanel();
}
author: Karsten Lentzsch version: $Revision: 1.2 $ See Also: ButtonStackBuilder See Also: com.jgoodies.forms.factories.ButtonBarFactory See Also: com.jgoodies.forms.util.LayoutStyle |