A non-visual builder for building consistent button bars that comply
with popular style guides. Utilizes the JGoodies
FormLayoutand honors the platform's
LayoutStyle regarding button sizes,
gap widths, and the default button order.
This is an improved version of the older
ButtonBarBuilder.
The ButtonBarBuilder2 has a simpler, safer, and more convenient API,
see below for a comparison.
ButtonBarBuilder2 vs. ButtonBarBuilder:
ButtonBarBuilder2 uses only 3 component types that can be added:
button, standard, and growing button, where
ButtonBarBuilder has button, fixed, and growing.
Also, the ButtonBarBuilder2 doesn't group buttons.
The layout of the ButtonBarBuilder and ButtonBarBuilder2 is the same
if all buttons are smaller than
LayoutStyle#getDefaultButtonWidth().
If some buttons are wider, ButtonBarBuilder2 will make only these buttons
wider, where the old ButtonBarBuilder makes all (gridded) buttons wider.
Examples:
// Build a right-aligned bar for: OK, Cancel, Apply
ButtonBarBuilder2 builder = new ButtonBarBuilder2();
builder.addGlue();
builder.addButton(okButton);
builder.addRelatedGap();
builder.addButton(cancelButton);
builder.addRelatedGap();
builder.addButton(applyButton);
return builder.getPanel();
// Add a sequence of related buttons
ButtonBarBuilder2 builder = new ButtonBarBuilder2();
builder.addGlue();
builder.addButton(okButton, cancelButton, applyButton);
return builder.getPanel();
// Add a sequence of related buttons for given Actions
ButtonBarBuilder2 builder = new ButtonBarBuilder2();
builder.addGlue();
builder.addButton(okAction, cancelAction, applyAction);
return builder.getPanel();
Buttons are added to a builder individually or as a sequence.
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.
#addButton(JComponent[]) honor the button order.
If you want to ignore the default button order, you can either
add individual buttons, or create a ButtonBarBuilder2 instance
with the order set to left-to-right. For the latter see
#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) {
ButtonBarBuilder2 builder = new ButtonBarBuilder2();
builder.addButton(help);
builder.addUnrelatedGap();
builder.addGlue();
builder.addButton(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 ButtonBarBuilder2()));
rowBuilder.append(buildButtonSequence(ButtonBarBuilder2.createLeftToRightBuilder()));
rowBuilder.append(buildIndividualButtons(new ButtonBarBuilder2()));
return rowBuilder.getPanel();
}
private Component buildButtonSequence(ButtonBarBuilder2 builder) {
builder.addButton(new JButton[] {
new JButton("One"),
new JButton("Two"),
new JButton("Three")
});
return builder.getPanel();
}
private Component buildIndividualButtons(ButtonBarBuilder2 builder) {
builder.addButton(new JButton("One"));
builder.addRelatedGap();
builder.addButton(new JButton("Two"));
builder.addRelatedGap();
builder.addButton(new JButton("Three"));
return builder.getPanel();
}