Class ButtonBar

All Implemented Interfaces:
Styleable, EventTarget, Skinnable

public class ButtonBar
extends Control
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 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.

Uniform button sizing

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.

Screenshots

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:

Mac OS:

Linux:

Code Samples

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:

ButtonBar Layout Table
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 ButtonBar.ButtonData enumeration for a description of what each of these characters mean. However, if your ButtonBar only consisted of ButtonBar.ButtonData.YES and ButtonBar.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");
 
Since:
JavaFX 8u40
See Also:
ButtonBar.ButtonData
  • Property Details

  • Field Details

  • Constructor Details

    • ButtonBar

      public ButtonBar()
      Creates a default ButtonBar instance using the default properties for the users operating system.
    • ButtonBar

      public ButtonBar​(String buttonOrder)
      Creates a ButtonBar with the given button order (refer to buttonOrderProperty() for more information).
      Parameters:
      buttonOrder - The button order to use in this button bar instance.
  • Method Details

    • setButtonData

      public static void setButtonData​(Node button, ButtonBar.ButtonData buttonData)
      Sets the given ButtonData on the given button. If this button is subsequently placed in a ButtonBar it will be placed in the correct position relative to all other buttons in the bar.
      Parameters:
      button - The button to annotate with the given ButtonBar.ButtonData value.
      buttonData - The ButtonData to designate the button as.
    • getButtonData

      public static ButtonBar.ButtonData getButtonData​(Node button)
      Returns the previously set ButtonData property on the given button. If this was never set, this method will return null.
      Parameters:
      button - The button to return the previously set ButtonData for.
      Returns:
      the previously set ButtonData property on the given button
    • setButtonUniformSize

      public static void setButtonUniformSize​(Node button, boolean uniformSize)
      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 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.

      Parameters:
      button - The button to include / exclude from uniform sizing.
      uniformSize - Boolean true to force uniform sizing on the button, false to exclude the button from uniform sizing.
    • isButtonUniformSize

      public static boolean isButtonUniformSize​(Node button)
      Returns whether the given node is part of the uniform sizing calculations or not. By default all nodes that have not opted out (via setButtonUniformSize(Node, boolean)) will return true here.
      Parameters:
      button - the button
      Returns:
      true if button is part of the uniform sizing calculations
    • createDefaultSkin

      protected Skin<?> createDefaultSkin()
      Create a new instance of the default skin for this control. This is called to create a skin for the control if no skin is provided via CSS -fx-skin or set explicitly in a sub-class with setSkin(...).
      Overrides:
      createDefaultSkin in class Control
      Returns:
      new instance of default skin for this control. If null then the control will have no skin unless one is provided by css.
    • getButtons

      public final ObservableList<Node> getButtons()
      Placing buttons inside this ObservableList will instruct the ButtonBar to position them relative to each other based on their specified ButtonBar.ButtonData. To set the ButtonData for a button, simply call setButtonData(Node, ButtonData), passing in the relevant ButtonData.
      Returns:
      A list containing all buttons currently in the button bar, and allowing for further buttons to be added or removed.
    • buttonOrderProperty

      public final StringProperty buttonOrderProperty()
      The order for the typical buttons in a standard button bar. It is one letter per ButtonBar.ButtonData enumeration value. Default button orders for operating systems are also available: BUTTON_ORDER_WINDOWS, BUTTON_ORDER_MAC_OS, and BUTTON_ORDER_LINUX.
      See Also:
      getButtonOrder(), setButtonOrder(String)
    • setButtonOrder

      public final void setButtonOrder​(String buttonOrder)
      Sets the button order
      Parameters:
      buttonOrder - The currently set button order, which by default will be the OS-specific button order.
    • getButtonOrder

      public final String getButtonOrder()
      Returns the current button order.
      Returns:
      The current button order.
    • buttonMinWidthProperty

      public final DoubleProperty buttonMinWidthProperty()
      Specifies the minimum width of all buttons placed in this button bar.
      See Also:
      getButtonMinWidth(), setButtonMinWidth(double)
    • setButtonMinWidth

      public final void setButtonMinWidth​(double value)
      Sets the minimum width of all buttons placed in this button bar.
      Parameters:
      value - the minimum width value
    • getButtonMinWidth

      public final double getButtonMinWidth()
      Returns the minimum width of all buttons placed in this button bar.
      Returns:
      the minimum width value
    • getInitialFocusTraversable

      protected Boolean getInitialFocusTraversable()
      Returns the initial focus traversable state of this control, for use by the JavaFX CSS engine to correctly set its initial value. This method is overridden as by default UI controls have focus traversable set to true, but that is not appropriate for this control.
      Overrides:
      getInitialFocusTraversable in class Control
      Returns:
      the initial focus traversable state of this control
      Since:
      9