Top Description Fields Constructors Methods
org.controlsfx.control

public Class SegmentedButton

extends ControlsFXControl
Class Inheritance
Imports
impl.org.controlsfx.skin.SegmentedButtonSkin, javafx.beans.property.ObjectProperty, .SimpleObjectProperty, javafx.collections.FXCollections, .ObservableList, javafx.scene.control.Skin, .ToggleButton, .ToggleGroup

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 Summary

Modifier and TypeField and Description
private final ObservableList<ToggleButton>
buttons

Private fields

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.

private final ObjectProperty<ToggleGroup>

Constructor Summary

AccessConstructor and Description
public
SegmentedButton()

Creates a default SegmentedButton instance with no buttons.

public
SegmentedButton(ToggleButton...
A varargs array of buttons to add into the SegmentedButton instance.
buttons
)

Creates a default SegmentedButton instance with the provided buttons inserted into it.

public
SegmentedButton(ObservableList<ToggleButton>
A list of buttons to add into the SegmentedButton instance.
buttons
)

Creates a default SegmentedButton instance with the provided buttons inserted into it.

Method Summary

Modifier and TypeMethod and Description
protected Skin<?>
createDefaultSkin()

Overrides javafx.scene.control.Control.createDefaultSkin.

Create a new instance of the default skin for this control.

public final ObservableList<ToggleButton>
getButtons()

Returns the list of buttons that this SegmentedButton will draw together into one 'grouped button'.

public ToggleGroup
getToggleGroup()

Returns: ToggleGroup used internally

public String
getUserAgentStylesheet()

Overrides javafx.scene.layout.Region.getUserAgentStylesheet.

An implementation may specify its own user-agent styles for this Region, and its children, by overriding this method.

public void
setToggleGroup(final ToggleGroup
ToggleGroup to be used internally
toggleGroup
)

public ObjectProperty<ToggleGroup>
toggleGroupProperty()

Returns: Property of the ToggleGroup used internally

Inherited from org.controlsfx.control.ControlsFXControl:
getUserAgentStylesheet

Field Detail

buttonsback to summary
private final ObservableList<ToggleButton> buttons

Private fields

STYLE_CLASS_DARKback to summary
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);
toggleGroupback to summary
private final ObjectProperty<ToggleGroup> toggleGroup

Constructor Detail

SegmentedButtonback to summary
public SegmentedButton()

Creates a default SegmentedButton instance with no buttons.

SegmentedButtonback to summary
public SegmentedButton(ToggleButton... buttons)

Creates a default SegmentedButton instance with the provided buttons inserted into it.

Parameters
buttons:ToggleButton[]

A varargs array of buttons to add into the SegmentedButton instance.

SegmentedButtonback to summary
public SegmentedButton(ObservableList<ToggleButton> buttons)

Creates a default SegmentedButton instance with the provided buttons inserted into it.

Parameters
buttons:ObservableList<ToggleButton>

A list of buttons to add into the SegmentedButton instance.

Method Detail

createDefaultSkinback to summary
protected Skin<?> createDefaultSkin()

Overrides javafx.scene.control.Control.createDefaultSkin.

Doc from javafx.scene.control.Control.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(...).

Annotations
@Override
getButtonsback to summary
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.

getToggleGroupback to summary
public ToggleGroup getToggleGroup()
Returns:ToggleGroup

ToggleGroup used internally

getUserAgentStylesheetback to summary
public String getUserAgentStylesheet()

Overrides javafx.scene.layout.Region.getUserAgentStylesheet.

Doc from javafx.scene.layout.Region.getUserAgentStylesheet.

An implementation may specify its own user-agent styles for this Region, and its children, by overriding this method. These styles are used in addition to whatever user-agent stylesheets are in use. This provides a mechanism for third parties to introduce styles for custom controls.

The URL is a hierarchical URI of the form [scheme:][//authority][path]. If the URL does not have a [scheme:] component, the URL is considered to be the [path] component only. Any leading '/' character of the [path] is ignored and the [path] is treated as a path relative to the root of the application's classpath.

Subclasses overriding this method should not assume any particular implementation approach as to the number and frequency with which it is called. For this reason, attempting any kind of dynamic implementation (i.e. returning different user agent stylesheet values) based on some state change is highly discouraged, as there is no guarantee when, or even if, this method will be called. Some JavaFX CSS implementations may choose to cache this response for an indefinite period of time, and therefore there should be no expectation around when this method is called.



package com.example.javafx.app;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class MyApp extends Application {

    @Override public void start(Stage stage) {
        Scene scene = new Scene(new Group());
        scene.getStylesheets().add("/com/example/javafx/app/mystyles.css");
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
For additional information about using CSS with the scene graph, see the CSS Reference Guide.
Annotations
@Override
setToggleGroupback to summary
public void setToggleGroup(final ToggleGroup toggleGroup)
Parameters
toggleGroup:ToggleGroup

ToggleGroup to be used internally

toggleGroupPropertyback to summary
public ObjectProperty<ToggleGroup> toggleGroupProperty()
Returns:ObjectProperty<ToggleGroup>

Property of the ToggleGroup used internally