Top Description Fields Constructors Methods
javafx.scene.control

public Class RadioMenuItem

extends MenuItem
implements Toggle
Class Inheritance
All Implemented Interfaces
javafx.scene.control.Toggle
Known Direct Subclasses
javafx.scene.control.skin.TabPaneSkin.TabMenuItem, com.sun.javafx.scene.control.GlobalMenuAdapter.RadioMenuItemAdapter
Imports
javafx.beans.property.BooleanProperty, .BooleanPropertyBase, .ObjectProperty, .ObjectPropertyBase, javafx.scene.Node

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 javafx.scene.control.RadioButton and javafx.scene.control.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);
Image of the RadioMenuItem control

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
MenuItem, Menu

Field Summary

Modifier and TypeField and Description
private static final String
private BooleanProperty
private static final String
private ObjectProperty<ToggleGroup>
toggleGroup

Represents the ToggleGroup that this RadioMenuItem belongs to.

Inherited from javafx.scene.control.MenuItem:
eventHandlerManagerMENU_VALIDATION_EVENT

Constructor Summary

AccessConstructor and Description
public
RadioMenuItem()

Constructs a RadioMenuItem with no display text.

public
RadioMenuItem(String
the display text
text
)

Constructs a RadioMenuItem and sets the display text with the specified text.

public
RadioMenuItem(String
the display text
text
,
Node
the graphic node
graphic
)

Constructs a RadioMenuItem and sets the display text with the specified text and sets the graphic Node to the given node.

Method Summary

Modifier and TypeMethod and Description
public final ToggleGroup
getToggleGroup()

Implements javafx.scene.control.Toggle.getToggleGroup.

Returns The ToggleGroup to which this Toggle belongs.

public final boolean
isSelected()

Implements javafx.scene.control.Toggle.isSelected.

Indicates whether this Toggle is selected.

public final BooleanProperty
selectedProperty()

Implements javafx.scene.control.Toggle.selectedProperty.

The selected state for this Toggle.

public final void
setSelected(boolean
true to make this Toggle selected.
value
)

Implements javafx.scene.control.Toggle.setSelected.

Sets this Toggle as selected or unselected.

public final void
setToggleGroup(ToggleGroup
The new ToggleGroup.
value
)

Implements javafx.scene.control.Toggle.setToggleGroup.

Sets the ToggleGroup to which this Toggle belongs.

public final ObjectProperty<ToggleGroup>
Inherited from javafx.scene.control.MenuItem:
acceleratorPropertyaddEventFilteraddEventHandlerbuildEventDispatchChaindisablePropertyfiregetAcceleratorgetCssMetaDatagetGraphicgetIdgetOnActiongetOnMenuValidationgetParentMenugetParentPopupgetPropertiesgetPseudoClassStatesgetStylegetStyleableNodegetStyleableParentgetStyleClassgetTextgetTypeSelectorgetUserDatagraphicPropertyidPropertyisDisableisMnemonicParsingisVisiblemnemonicParsingPropertyonActionPropertyonMenuValidationPropertyparentMenuPropertyparentPopupPropertyremoveEventFilterremoveEventHandlersetAcceleratorsetDisablesetGraphicsetIdsetMnemonicParsingsetOnActionsetOnMenuValidationsetParentMenusetParentPopupsetStylesetTextsetUserDatasetVisiblestylePropertytextPropertytoStringvisibleProperty

Field Detail

DEFAULT_STYLE_CLASSback to summary
private static final String DEFAULT_STYLE_CLASS

Hides javafx.scene.control.MenuItem.DEFAULT_STYLE_CLASS.

selectedback to summary
private BooleanProperty selected
STYLE_CLASS_SELECTEDback to summary
private static final String STYLE_CLASS_SELECTED
toggleGroupback to summary
private ObjectProperty<ToggleGroup> toggleGroup

Represents the ToggleGroup that this RadioMenuItem belongs to.

Constructor Detail

RadioMenuItemback to summary
public RadioMenuItem()

Constructs a RadioMenuItem with no display text.

RadioMenuItemback to summary
public RadioMenuItem(String text)

Constructs a RadioMenuItem and sets the display text with the specified text.

Parameters
text:String

the display text

RadioMenuItemback to summary
public RadioMenuItem(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.

Parameters
text:String

the display text

graphic:Node

the graphic node

Method Detail

getToggleGroupback to summary
public final ToggleGroup getToggleGroup()

Implements javafx.scene.control.Toggle.getToggleGroup.

Doc from javafx.scene.control.Toggle.getToggleGroup.

Returns The ToggleGroup to which this Toggle belongs.

Returns:ToggleGroup

The ToggleGroup to which this Toggle belongs.

Annotations
@Override
isSelectedback to summary
public final boolean isSelected()

Implements javafx.scene.control.Toggle.isSelected.

Doc from javafx.scene.control.Toggle.isSelected.

Indicates whether this Toggle is selected.

Returns:boolean

true if this Toggle is selected.

Annotations
@Override
selectedPropertyback to summary
public final BooleanProperty selectedProperty()

Implements javafx.scene.control.Toggle.selectedProperty.

Doc from javafx.scene.control.Toggle.selectedProperty.

The selected state for this Toggle.

Returns:BooleanProperty

the selected property

Annotations
@Override
setSelectedback to summary
public final void setSelected(boolean value)

Implements javafx.scene.control.Toggle.setSelected.

Doc from javafx.scene.control.Toggle.setSelected.

Sets this Toggle as selected or unselected.

Parameters
value:boolean

true to make this Toggle selected.

Annotations
@Override
setToggleGroupback to summary
public final void setToggleGroup(ToggleGroup value)

Implements javafx.scene.control.Toggle.setToggleGroup.

Doc from javafx.scene.control.Toggle.setToggleGroup.

Sets the ToggleGroup to which this Toggle belongs.

Parameters
value:ToggleGroup

The new ToggleGroup.

Annotations
@Override
toggleGroupPropertyback to summary
public final ObjectProperty<ToggleGroup> toggleGroupProperty()

Implements javafx.scene.control.Toggle.toggleGroupProperty.

Doc from javafx.scene.control.Toggle.toggleGroupProperty.

The ToggleGroup to which this Toggle belongs.

Returns:ObjectProperty<ToggleGroup>

the toggle group property

Annotations
@Override