A ButtonBar is essentially a HBox
, with the additional functionality
for operating system specific button placement. In other words, any Node may
be annotated (via the ButtonBar.setButtonData(Node, ButtonData)
method, placed inside a ButtonBar (via the getButtons()
list), and will
then be positioned relative to all other nodes in the button list based on their
annotations, as well as the overarching
button order
specified for the ButtonBar.
By default all buttons are uniformly sized in a ButtonBar, meaning that all
buttons take the width of the widest button. It is possible to opt-out of this
on a per-button basis, but calling the setButtonUniformSize(Node, boolean)
method with
a boolean value of false.
If a button is excluded from uniform sizing, it is both excluded from being resized away from its preferred size, and also excluded from the measuring process, so its size will not influence the maximum size calculated for all buttons in the ButtonBar.
Because a ButtonBar comes with built-in support for Windows, Mac OS and Linux, there are three screenshots shown below, with the same buttons laid out on each of the three operating systems.
Windows:
Instantiating and using the ButtonBar is simple, simply do the following:
// Create the ButtonBar instance
ButtonBar buttonBar = new ButtonBar();
// Create the buttons to go into the ButtonBar
Button yesButton = new Button("Yes");
ButtonBar.setButtonData(yesButton, ButtonData.YES);
Button noButton = new Button("No");
ButtonBar.setButtonData(noButton, ButtonData.NO);
// Add buttons to the ButtonBar
buttonBar.getButtons().addAll(yesButton, noButton);
The code sample above will position the Yes and No buttons relative to the users operating system. This means that on Windows and Linux the Yes button will come before the No button, whereas on Mac OS it'll be No and then Yes.
In most cases the OS-specific layout is the best choice, but in cases
where you want a custom layout, this is achieved be modifying the
button order property
. These are cryptic-looking
strings that are shorthand representations for the button order. The built-in
orders for Windows, Mac OS and Linux are:
Operating System | Button Order |
---|---|
Windows | L_E+U+FBXI_YNOCAH_R |
Mac OS | L_HE+U+FBIX_NCYOA_R |
Linux | L_HE+UNYACBXIO_R |
You should refer to the ButtonData
enumeration for a description of
what each of these characters mean. However, if your ButtonBar only consisted
of ButtonData.YES
and ButtonData.NO
buttons, you always
wanted the yes buttons before the no buttons, and you wanted the buttons to
be right-aligned
, you could do the following:
// Create the ButtonBar instance
ButtonBar buttonBar = new ButtonBar();
// Set the custom button order
buttonBar.setButtonOrder("+YN");
extends
ButtonData