d 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. {@link #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 {@link #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(); }
@author Karsten Lentzsch
@version $Revision: 1.9 $
@see ButtonStackBuilder
@see com.jgoodies.forms.factories.ButtonBarFactory
@see com.jgoodies.forms.util.LayoutStyle