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:
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);
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);
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);
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);
ToggleButton, ToggleGroup
| Modifier and Type | Field and Description |
|---|---|
| private final ObservableList | 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 |
| Access | Constructor and Description |
|---|---|
| public | |
| 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. |
| Modifier and Type | Method and Description |
|---|---|
| protected Skin | createDefaultSkin()
Overrides javafx. Create a new instance of the default skin for this control. |
| public final ObservableList | getButtons()
Returns the list of buttons that this SegmentedButton will draw together into one 'grouped button'. |
| public ToggleGroup | |
| public String | getUserAgentStylesheet()
Overrides javafx. An implementation may specify its own user-agent styles for this Region, and its children, by overriding this method. |
| public void | |
| public ObjectProperty |
| buttons | back to summary |
|---|---|
| private final ObservableList<ToggleButton> buttons Private fields | |
| STYLE_CLASS_DARK | back 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:
| |
| toggleGroup | back to summary |
|---|---|
| private final ObjectProperty<ToggleGroup> toggleGroup | |
| SegmentedButton | back to summary |
|---|---|
| public SegmentedButton() Creates a default SegmentedButton instance with no buttons. | |
| SegmentedButton | back to summary |
|---|---|
| public SegmentedButton(ToggleButton... buttons) Creates a default SegmentedButton instance with the provided buttons inserted into it.
| |
| SegmentedButton | back to summary |
|---|---|
| public SegmentedButton(ObservableList<ToggleButton> buttons) Creates a default SegmentedButton instance with the provided buttons inserted into it.
| |
| createDefaultSkin | back to summary |
|---|---|
| protected Skin Overrides javafx. Doc from javafx. 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
| |
| getButtons | back to summary |
|---|---|
| public final ObservableList 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 | |
| getToggleGroup | back to summary |
|---|---|
public ToggleGroup getToggleGroup()
| |
| getUserAgentStylesheet | back to summary |
|---|---|
| public String getUserAgentStylesheet() Overrides javafx. Doc from javafx. 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.
For additional information about using CSS with the scene graph,
see the CSS Reference Guide.
| |
| setToggleGroup | back to summary |
|---|---|
public void setToggleGroup(final ToggleGroup toggleGroup)
| |
| toggleGroupProperty | back to summary |
|---|---|
public ObjectProperty
| |