Class SegmentedButton

  • All Implemented Interfaces:
    Styleable, EventTarget, Skinnable

    public class SegmentedButton
    extends Control
    The SegmentedButton is a simple control that forces together a group of ToggleButton instances such that they appear as one collective button (with sub-buttons), rather than as individual buttons. This is better clarified with a picture:
    Screenshot of SegmentedButton

    Code Samples

    There is very little API on this control, you essentially create ToggleButton instances as per usual (and don't bother putting them into a ToggleGroup, as this is done by the SegmentedButton itself), and then you place these buttons inside the buttons list. The long-hand way to code this is as follows:

     
     ToggleButton b1 = new ToggleButton("day");
     ToggleButton b2 = new ToggleButton("week");
     ToggleButton b3 = new ToggleButton("month");
     ToggleButton b4 = new ToggleButton("year");
           
     SegmentedButton segmentedButton = new SegmentedButton();    
     segmentedButton.getButtons().addAll(b1, b2, b3, b4);

    A slightly shorter way of doing this is to pass the ToggleButton instances into the varargs constructor, as such:

    SegmentedButton segmentedButton = new SegmentedButton(b1, b2, b3, b4);

    Custom ToggleGroup

    It is possible to configure the ToggleGroup, which is used internally. By setting the ToggleGroup to null, the control will allow multiple selections.

     
     SegmentedButton segmentedButton = new SegmentedButton();
     segmentedButton.setToggleGroup(null);
     

    Alternative Styling

    As is visible in the screenshot at the top of this class documentation, there are two different styles supported by the SegmentedButton control. Firstly, there is the default style based on the JavaFX Modena look. The alternative style is what is currently referred to as the 'dark' look. To enable this functionality, simply do the following:

     
     SegmentedButton segmentedButton = new SegmentedButton();   
     segmentedButton.getStyleClass().add(SegmentedButton.STYLE_CLASS_DARK);
     

    Resizable Range

    By default, the maximum width and height of a SegmentedButton match its preferred width and height. Thus, the SegmentedButton only fills the area which is necessary to display the contained buttons. To change this behavior, the following code can be used:

     
     SegmentedButton segmentedButton = new SegmentedButton();
     segmentedButton.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
     
    See Also:
    ToggleButton, ToggleGroup
    • Field Detail

      • STYLE_CLASS_DARK

        public static final String STYLE_CLASS_DARK
        An alternative styling for the segmented button, with a darker pressed color which stands out more than the default modena styling. Refer to the class documentation for details on how to use (and screenshots), but in short, simply do the following to get the dark styling:
         
         SegmentedButton segmentedButton = new SegmentedButton();   
         segmentedButton.getStyleClass().add(SegmentedButton.STYLE_CLASS_DARK);
         
        See Also:
        Constant Field Values
    • Constructor Detail

      • SegmentedButton

        public SegmentedButton()
        Creates a default SegmentedButton instance with no buttons.
      • SegmentedButton

        public SegmentedButton​(ToggleButton... buttons)
        Creates a default SegmentedButton instance with the provided buttons inserted into it.
        Parameters:
        buttons - A varargs array of buttons to add into the SegmentedButton instance.
      • SegmentedButton

        public SegmentedButton​(ObservableList<ToggleButton> buttons)
        Creates a default SegmentedButton instance with the provided buttons inserted into it.
        Parameters:
        buttons - A list of buttons to add into the SegmentedButton instance.
    • Method Detail

      • getButtons

        public final ObservableList<ToggleButton> getButtons()
        Returns the list of buttons that this SegmentedButton will draw together into one 'grouped button'. It is possible to modify this list to add or remove ToggleButton instances, as shown in the javadoc documentation for this class.
      • getToggleGroup

        public ToggleGroup getToggleGroup()
        Returns:
        ToggleGroup used internally
      • setToggleGroup

        public void setToggleGroup​(ToggleGroup toggleGroup)
        Parameters:
        toggleGroup - ToggleGroup to be used internally
      • getUserAgentStylesheet

        protected final String getUserAgentStylesheet​(Class<?> clazz,
                                                      String fileName)
        A helper method that ensures that the resource based lookup of the user agent stylesheet only happens once. Caches the external form of the resource.
        Parameters:
        clazz - the class used for the resource lookup
        fileName - the name of the user agent stylesheet
        Returns:
        the external form of the user agent stylesheet (the path)