Class RadioMenuItem
- javafx.scene.control.MenuItem
-
- javafx.scene.control.RadioMenuItem
All Implemented Interfaces:
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. -
Property Summary
All MethodsInstance MethodsConcrete Methods Type Property and Description BooleanPropertyselectedThe selected state for thisToggle.ObjectProperty<ToggleGroup>toggleGroupRepresents theToggleGroupthat this RadioMenuItem belongs to.-
Properties inherited from class javafx.scene.control.MenuItem
accelerator, disable, graphic, id, mnemonicParsing, onAction, onMenuValidation, parentMenu, parentPopup, style, text, visible
-
-
Field Summary
-
Fields inherited from class javafx.scene.control.MenuItem
MENU_VALIDATION_EVENT
-
-
Constructor Summary
Constructors Constructor and Description RadioMenuItem()Constructs a RadioMenuItem with no display text.RadioMenuItem(String text)Constructs a RadioMenuItem and sets the display text with the specified text.RadioMenuItem(String text, Node graphic)Constructs a RadioMenuItem and sets the display text with the specified text and sets the graphicNodeto the given node.
-
Method Summary
All MethodsInstance MethodsConcrete Methods Modifier and Type Method and Description ToggleGroupgetToggleGroup()Gets the value of the property toggleGroup.booleanisSelected()Gets the value of the property selected.BooleanPropertyselectedProperty()The selected state for thisToggle.voidsetSelected(boolean value)Sets the value of the property selected.voidsetToggleGroup(ToggleGroup value)Sets the value of the property toggleGroup.ObjectProperty<ToggleGroup>toggleGroupProperty()Represents theToggleGroupthat this RadioMenuItem belongs to.-
Methods inherited from class javafx.scene.control.MenuItem
acceleratorProperty, addEventHandler, buildEventDispatchChain, disableProperty, fire, getAccelerator, getCssMetaData, getGraphic, getId, getOnAction, getOnMenuValidation, getParentMenu, getParentPopup, getProperties, getPseudoClassStates, getStyle, 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
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface javafx.scene.control.Toggle
getProperties, getUserData, setUserData
-
-
Property Detail
-
toggleGroup
public final ObjectProperty<ToggleGroup> toggleGroupProperty
Represents theToggleGroupthat this RadioMenuItem belongs to.Specified by:
toggleGroupPropertyin interfaceToggleSee Also:
-
selected
public final BooleanProperty selectedProperty
Specified by:
selectedPropertyin interfaceToggleSee Also:
-
-
Constructor Detail
-
RadioMenuItem
public RadioMenuItem()
Constructs a RadioMenuItem with no display text.
-
RadioMenuItem
public RadioMenuItem(String text)
Constructs a RadioMenuItem and sets the display text with the specified text.
-
RadioMenuItem
public RadioMenuItem(String text, Node graphic)Constructs a RadioMenuItem and sets the display text with the specified text and sets the graphicNodeto the given node.
-
-
Method Detail
-
setToggleGroup
public final void setToggleGroup(ToggleGroup value)
Sets the value of the property toggleGroup.Specified by:
setToggleGroupin interfaceToggleProperty description:
Represents the
ToggleGroupthat this RadioMenuItem belongs to.Parameters:
value- The newToggleGroup.
-
getToggleGroup
public final ToggleGroup getToggleGroup()
Gets the value of the property toggleGroup.Specified by:
getToggleGroupin interfaceToggleProperty description:
Represents the
ToggleGroupthat this RadioMenuItem belongs to.Returns:
The
ToggleGroupto which thisTogglebelongs.
-
toggleGroupProperty
public final ObjectProperty<ToggleGroup> toggleGroupProperty()
Represents theToggleGroupthat this RadioMenuItem belongs to.Specified by:
toggleGroupPropertyin interfaceToggleSee Also:
-
setSelected
public final void setSelected(boolean value)
Sets the value of the property selected.Specified by:
setSelectedin interfaceToggleParameters:
value-trueto make thisToggleselected.
-
isSelected
public final boolean isSelected()
Gets the value of the property selected.Specified by:
isSelectedin interfaceToggleReturns:
trueif thisToggleis selected.
-
selectedProperty
public final BooleanProperty selectedProperty()
Description copied from interface:ToggleThe selected state for thisToggle.Specified by:
selectedPropertyin interfaceToggleSee Also:
-
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.