public class RadioMenuItem extends MenuItem implements Toggle
A RadioMenuItem is a MenuItem
that can be toggled (it uses
the Toggle
mixin). This means that
RadioMenuItem has an API very similar in nature to other controls that use
Toggle
, such as
RadioButton
and
ToggleButton
. RadioMenuItem is
specifically designed for use within a Menu
, so refer to that class
API documentation for more information on how to add a RadioMenuItem into it.
To create a simple, ungrouped RadioMenuItem, do the following:
RadioMenuItem radioItem = new RadioMenuItem("radio text");
radioItem.setSelected(false);
radioItem.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("radio toggled");
}
});
The problem with the example above is that this offers no benefit over using
a normal MenuItem. As already mentioned, the purpose of a
RadioMenuItem is to offer
multiple choices to the user, and only allow for one of these choices to be
selected at any one time (i.e. the selection should be mutually exclusive).
To achieve this, you can place zero or more RadioMenuItem's into groups. When
in groups, only one RadioMenuItem at a time within that group can be selected.
To put two RadioMenuItem instances into the same group, simply assign them
both the same value for toggleGroup
. For example:
ToggleGroup toggleGroup = new ToggleGroup();
RadioMenuItem radioItem1 = new RadioMenuItem("Option 1");
radioItem.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("radio toggled");
}
});
radioItem1.setToggleGroup(toggleGroup);
RadioMenuItem radioItem2 = new RadioMenuItem("Option 2");
radioItem.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("radio toggled");
}
});
radioItem2.setToggleGroup(toggleGroup);
In this example, with both RadioMenuItem's assigned to the same
ToggleGroup
, only one item may be
selected at any one time, and should
the selection change, the ToggleGroup will take care of deselecting the
previous item.Type | Property and Description |
---|---|
BooleanProperty |
selected
The selected state for this
Toggle . |
<any> |
toggleGroup
Represents the
ToggleGroup that this RadioMenuItem belongs to. |
acceleratorProperty, disableProperty, graphicProperty, idProperty, mnemonicParsingProperty, onActionProperty, onMenuValidationProperty, parentMenuProperty, parentPopupProperty, styleProperty, textProperty, visibleProperty
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
DEFAULT_STYLE_CLASS
*
Stylesheet Handling *
*
|
private BooleanProperty |
selected |
private static java.lang.String |
STYLE_CLASS_SELECTED |
private <any> |
toggleGroup
Represents the
ToggleGroup that this RadioMenuItem belongs to. |
eventHandlerManager, MENU_VALIDATION_EVENT
Constructor and Description |
---|
RadioMenuItem()
Constructs a RadioMenuItem with no display text.
|
RadioMenuItem(java.lang.String text)
Constructs a RadioMenuItem and sets the display text with the specified text.
|
RadioMenuItem(java.lang.String text,
Node graphic)
Constructs a RadioMenuItem and sets the display text with the specified text
and sets the graphic
Node to the given node. |
Modifier and Type | Method and Description |
---|---|
ToggleGroup |
getToggleGroup()
Gets the value of the property toggleGroup.
|
boolean |
isSelected()
Gets the value of the property selected.
|
BooleanProperty |
selectedProperty()
The selected state for this
Toggle . |
void |
setSelected(boolean value)
Sets the value of the property selected.
|
void |
setToggleGroup(ToggleGroup value)
Sets the value of the property toggleGroup.
|
<any> |
toggleGroupProperty()
Represents the
ToggleGroup that this RadioMenuItem belongs to. |
acceleratorProperty, addEventHandler, buildEventDispatchChain, disableProperty, fire, getAccelerator, getCssMetaData, getGraphic, getId, getOnAction, getOnMenuValidation, getParentMenu, getParentPopup, getProperties, getPseudoClassStates, getStyle, getStyleableNode, getStyleableParent, getStyleClass, getText, getTypeSelector, getUserData, graphicProperty, idProperty, isDisable, isMnemonicParsing, isVisible, mnemonicParsingProperty, onActionProperty, onMenuValidationProperty, parentMenuProperty, parentPopupProperty, removeEventHandler, setAccelerator, setDisable, setGraphic, setId, setMnemonicParsing, setOnAction, setOnMenuValidation, setParentMenu, setParentPopup, setStyle, setText, setUserData, setVisible, styleProperty, textProperty, toString, visibleProperty
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getProperties, getUserData, setUserData
public final <any> toggleGroupProperty
ToggleGroup
that this RadioMenuItem belongs to.toggleGroupProperty
in interface Toggle
getToggleGroup()
,
setToggleGroup(ToggleGroup)
public final BooleanProperty selectedProperty
selectedProperty
in interface Toggle
isSelected()
,
setSelected(boolean)
private <any> toggleGroup
ToggleGroup
that this RadioMenuItem belongs to.private BooleanProperty selected
private static final java.lang.String DEFAULT_STYLE_CLASS
private static final java.lang.String STYLE_CLASS_SELECTED
public RadioMenuItem()
public RadioMenuItem(java.lang.String text)
text
- the display textpublic RadioMenuItem(java.lang.String text, Node graphic)
Node
to the given node.text
- the display textgraphic
- the graphic nodepublic final void setToggleGroup(ToggleGroup value)
setToggleGroup
in interface Toggle
value
- The new ToggleGroup
.public final ToggleGroup getToggleGroup()
getToggleGroup
in interface Toggle
ToggleGroup
to which this Toggle
belongs.public final <any> toggleGroupProperty()
ToggleGroup
that this RadioMenuItem belongs to.toggleGroupProperty
in interface Toggle
getToggleGroup()
,
setToggleGroup(ToggleGroup)
public final void setSelected(boolean value)
setSelected
in interface Toggle
value
- true
to make this Toggle
selected.public final boolean isSelected()
isSelected
in interface Toggle
true
if this Toggle
is selected.public final BooleanProperty selectedProperty()
Toggle
Toggle
.selectedProperty
in interface Toggle
isSelected()
,
setSelected(boolean)