Class RadioMenuItem
- All Implemented Interfaces:
Styleable, EventTarget, 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(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");
radioItem1.setOnAction(e -> System.out.println("radio1 toggled"));
radioItem1.setToggleGroup(toggleGroup);
RadioMenuItem radioItem2 = new RadioMenuItem("Option 2");
radioItem2.setOnAction(e -> System.out.println("radio2 toggled"));
radioItem2.setToggleGroup(toggleGroup);
Menu menu = new Menu("Selection");
menu.getItems().addAll(radioItem1, radioItem2);
MenuBar menuBar = new MenuBar(menu);
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.
- Since:
- JavaFX 2.0
- See Also:
-
Property Summary
PropertiesTypePropertyDescriptionfinal BooleanPropertyThe selected state for thisToggle.final ObjectProperty<ToggleGroup> Represents theToggleGroupthat this RadioMenuItem belongs to.Properties declared in class MenuItem
accelerator, disable, graphic, id, mnemonicParsing, onAction, onMenuValidation, parentMenu, parentPopup, style, text, visibleTypePropertyDescriptionfinal ObjectProperty<KeyCombination> The accelerator property enables accessing the associated action in one keystroke.final BooleanPropertySets the individual disabled state of this MenuItem.final ObjectProperty<Node> An optional graphic for theMenuItem.final StringPropertyThe id of this MenuItem.final BooleanPropertyDetermines whether the mnemonic character in the menu item text will be parsed.final ObjectProperty<EventHandler<ActionEvent>> The action, which is invoked whenever the MenuItem is fired.final ObjectProperty<EventHandler<Event>> The event handler that is associated with invocation of an accelerator for a MenuItem.final ReadOnlyObjectProperty<Menu> This is theMenuin which thisMenuItemexists.This is theContextMenuin which thisMenuItemexists.final StringPropertyA string representation of the CSS style associated with this specific MenuItem.final StringPropertyThe text to display in theMenuItem.final BooleanPropertySpecifies whether this MenuItem should be rendered as part of the scene graph. -
Field Summary
Fields declared in class MenuItem
MENU_VALIDATION_EVENTModifier and TypeFieldDescriptionCalled when a accelerator for the Menuitem is invoked -
Constructor Summary
ConstructorsConstructorDescriptionConstructs 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
Modifier and TypeMethodDescriptionfinal ToggleGroupGets the value of thetoggleGroupproperty.final booleanGets the value of theselectedproperty.final BooleanPropertyThe selected state for thisToggle.final voidsetSelected(boolean value) Sets the value of theselectedproperty.final voidsetToggleGroup(ToggleGroup value) Sets the value of thetoggleGroupproperty.final ObjectProperty<ToggleGroup> Represents theToggleGroupthat this RadioMenuItem belongs to.Methods declared in class MenuItem
acceleratorProperty, 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, setAccelerator, setDisable, setGraphic, setId, setMnemonicParsing, setOnAction, setOnMenuValidation, setParentMenu, setParentPopup, setStyle, setText, setUserData, setVisible, styleProperty, textProperty, visiblePropertyModifier and TypeMethodDescriptionfinal ObjectProperty<KeyCombination> The accelerator property enables accessing the associated action in one keystroke.Construct an event dispatch chain for this target.final BooleanPropertySets the individual disabled state of this MenuItem.voidfire()Fires a new ActionEvent.final KeyCombinationGets the value of theacceleratorproperty.List<CssMetaData<? extends Styleable, ?>> The CssMetaData of this Styleable.final NodeGets the value of thegraphicproperty.final StringgetId()Gets the value of theidproperty.final EventHandler<ActionEvent> Gets the value of theonActionproperty.final EventHandler<Event> Gets the value of theonMenuValidationproperty.final MenuGets the value of theparentMenuproperty.final ContextMenuGets the value of theparentPopupproperty.Returns an observable map of properties on this menu item for use primarily by application developers.final ObservableSet<PseudoClass> Return the pseudo-class state of this Styleable.final StringgetStyle()Gets the value of thestyleproperty.Return the parent of this Styleable, or null if there is no parent.A list of String identifiers which can be used to logically group Nodes, specifically for an external style engine.final StringgetText()Gets the value of thetextproperty.The type of thisStyleablethat is to be used in selector matching.final ObjectProperty<Node> An optional graphic for theMenuItem.final StringPropertyThe id of this MenuItem.final booleanGets the value of thedisableproperty.final booleanGets the value of themnemonicParsingproperty.final booleanGets the value of thevisibleproperty.final BooleanPropertyDetermines whether the mnemonic character in the menu item text will be parsed.final ObjectProperty<EventHandler<ActionEvent>> The action, which is invoked whenever the MenuItem is fired.final ObjectProperty<EventHandler<Event>> The event handler that is associated with invocation of an accelerator for a MenuItem.final ReadOnlyObjectProperty<Menu> This is theMenuin which thisMenuItemexists.This is theContextMenuin which thisMenuItemexists.final voidsetAccelerator(KeyCombination value) Sets the value of theacceleratorproperty.final voidsetDisable(boolean value) Sets the value of thedisableproperty.final voidsetGraphic(Node value) Sets the value of thegraphicproperty.final voidSets the value of theidproperty.final voidsetMnemonicParsing(boolean value) Sets the value of themnemonicParsingproperty.final voidsetOnAction(EventHandler<ActionEvent> value) Sets the value of theonActionproperty.final voidsetOnMenuValidation(EventHandler<Event> value) Sets the value of theonMenuValidationproperty.protected final voidsetParentMenu(Menu value) Sets the value of theparentMenuproperty.protected final voidsetParentPopup(ContextMenu value) Sets the value of theparentPopupproperty.final voidSets the value of thestyleproperty.final voidSets the value of thetextproperty.voidsetUserData(Object value) Stores a user object that contains data at their discretion.final voidsetVisible(boolean value) Sets the value of thevisibleproperty.final StringPropertyA string representation of the CSS style associated with this specific MenuItem.final StringPropertyThe text to display in theMenuItem.final BooleanPropertySpecifies whether this MenuItem should be rendered as part of the scene graph.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods declared in interface EventTarget
addEventFilter, addEventHandler, removeEventFilter, removeEventHandlerModifier and TypeMethodDescriptiondefault <E extends Event>
voidaddEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Registers an event filter for this target.default <E extends Event>
voidaddEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Registers an event handler for this target.default <E extends Event>
voidremoveEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Unregisters a previously registered event filter from this target.default <E extends Event>
voidremoveEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Unregisters a previously registered event handler from this target.Methods declared in interface Styleable
getStyleableNodeModifier and TypeMethodDescriptiondefault NodeReturns the Node that represents this Styleable object.Methods declared in interface Toggle
getProperties, getUserData, setUserDataModifier and TypeMethodDescriptionReturns an observable map of properties on this toggle for use primarily by application developers.Returns a previously set Object property, or null if no such property has been set using theNode.setUserData(java.lang.Object)method.voidsetUserData(Object value) Convenience method for setting a single Object property that can be retrieved at a later date.
-
Property Details
-
toggleGroup
Represents theToggleGroupthat this RadioMenuItem belongs to.- Specified by:
toggleGroupPropertyin interfaceToggle- Returns:
- the toggle group property
- See Also:
-
selected
- Specified by:
selectedPropertyin interfaceToggle- Returns:
- the selected property
- See Also:
-
-
Constructor Details
-
RadioMenuItem
public RadioMenuItem()Constructs a RadioMenuItem with no display text. -
RadioMenuItem
Constructs a RadioMenuItem and sets the display text with the specified text.- Parameters:
text- the display text
-
RadioMenuItem
-
-
Method Details
-
setToggleGroup
Sets the value of thetoggleGroupproperty.- Specified by:
setToggleGroupin interfaceToggle- Property description:
- Represents the
ToggleGroupthat this RadioMenuItem belongs to. - Parameters:
value- the value for thetoggleGroupproperty- See Also:
-
getToggleGroup
Gets the value of thetoggleGroupproperty.- Specified by:
getToggleGroupin interfaceToggle- Property description:
- Represents the
ToggleGroupthat this RadioMenuItem belongs to. - Returns:
- the value of the
toggleGroupproperty - See Also:
-
toggleGroupProperty
Represents theToggleGroupthat this RadioMenuItem belongs to.- Specified by:
toggleGroupPropertyin interfaceToggle- Returns:
- the
toggleGroupproperty - See Also:
-
setSelected
public final void setSelected(boolean value) Sets the value of theselectedproperty.- Specified by:
setSelectedin interfaceToggle- Property description:
- Parameters:
value- the value for theselectedproperty- See Also:
-
isSelected
public final boolean isSelected()Gets the value of theselectedproperty.- Specified by:
isSelectedin interfaceToggle- Property description:
- Returns:
- the value of the
selectedproperty - See Also:
-
selectedProperty
Description copied from interface:ToggleThe selected state for thisToggle.- Specified by:
selectedPropertyin interfaceToggle- Returns:
- the
selectedproperty - See Also:
-