APIdia Launch Partner
Top Description Fields Constructors Methods
javafx.scene.control

public Class MenuItem

extends Object
implements EventTarget, Styleable
Class Inheritance
All Implemented Interfaces
javafx.css.Styleable, javafx.event.EventTarget
Known Direct Subclasses
javafx.scene.control.RadioMenuItem, javafx.scene.control.Menu, com.sun.javafx.scene.control.GlobalMenuAdapter.MenuItemAdapter, com.sun.javafx.scene.control.behavior.TextInputControlBehavior.ContextMenuItem, javafx.scene.control.CheckMenuItem, javafx.scene.control.CustomMenuItem
Annotations
@IDProperty:id
Imports
com.sun.javafx.beans.IDProperty, javafx.collections.ObservableSet, .FXCollections, .ObservableList, .ObservableMap, javafx.css.PseudoClass, .Styleable, .CssMetaData, javafx.beans.property.BooleanProperty, .ObjectProperty, .ObjectPropertyBase, .SimpleBooleanProperty, .SimpleObjectProperty, .SimpleStringProperty, .StringProperty, .ReadOnlyObjectProperty, .ReadOnlyObjectWrapper, javafx.event.ActionEvent, .Event, .EventDispatchChain, .EventHandler, .EventTarget, .EventType, javafx.scene.Node, .Parent, javafx.scene.input.KeyCombination, com.sun.javafx.event.EventHandlerManager, com.sun.javafx.scene.control.ContextMenuContent, javafx.scene.control.skin.ContextMenuSkin, java.util.Collections, .HashMap, .List

MenuItem is intended to be used in conjunction with Menu to provide options to users. MenuItem serves as the base class for the bulk of JavaFX menus API. It has a display text property, as well as an optional graphic node that can be set on it. The accelerator property enables accessing the associated action in one keystroke. Also, as with the Button control, by using the setOnAction method, you can have an instance of MenuItem perform any action you wish.

Note

Whilst any size of graphic can be inserted into a MenuItem, the most commonly used size in most applications is 16x16 pixels. This is the recommended graphic dimension to use if you're using the default style provided by JavaFX.

To create a MenuItem is simple:

MenuItem menuItem = new MenuItem("Open");
menuItem.setOnAction(e -> System.out.println("Opening Database Connection..."));
Circle graphic = new Circle(8);
graphic.setFill(Color.GREEN);
menuItem.setGraphic(graphic);

Menu menu = new Menu("File");
menu.getItems().add(menuItem);
MenuBar menuBar = new MenuBar(menu);
Image of the MenuItem control
Since
JavaFX 2.0
See Also
Menu

Field Summary

Modifier and TypeField and Description
private ObjectProperty<KeyCombination>
accelerator

The accelerator property enables accessing the associated action in one keystroke.

private static final String
private BooleanProperty
disable

Sets the individual disabled state of this MenuItem.

pack-priv final EventHandlerManager
private ObjectProperty<Node>
graphic

An optional graphic for the MenuItem.

private StringProperty
id

The id of this MenuItem.

private BooleanProperty
mnemonicParsing

MnemonicParsing property to enable/disable text parsing.

private ObjectProperty<EventHandler<ActionEvent>>
onAction

The action, which is invoked whenever the MenuItem is fired.

private ObjectProperty<EventHandler<Event>>
onMenuValidation

The event handler that is associated with invocation of an accelerator for a MenuItem.

private ReadOnlyObjectWrapper<Menu>
parentMenu

This is the Menu in which this MenuItem exists.

private ReadOnlyObjectWrapper<ContextMenu>
parentPopup

This is the ContextMenu in which this MenuItem exists.

private ObservableMap<Object, Object>
private StringProperty
style

A string representation of the CSS style associated with this specific MenuItem.

private final ObservableList<String>
private StringProperty
text

The text to display in the MenuItem.

private Object
private BooleanProperty
visible

Specifies whether this MenuItem should be rendered as part of the scene graph.

Constructor Summary

AccessConstructor and Description

Method Summary

Modifier and TypeMethod and Description
public final ObjectProperty<KeyCombination>
public <E extends Event> void
addEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter)

Overrides default javafx.event.EventTarget.addEventFilter.

Registers an event filter for this target.

public <E extends Event> void
addEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler)

Overrides default javafx.event.EventTarget.addEventHandler.

Registers an event handler for this target.

public EventDispatchChain
buildEventDispatchChain(EventDispatchChain tail)

Implements javafx.event.EventTarget.buildEventDispatchChain.

Construct an event dispatch chain for this target.

public final BooleanProperty
public void
fire()

Fires a new ActionEvent.

public final KeyCombination
public List<CssMetaData<? extends Styleable, ?>>
getCssMetaData()

Implements javafx.css.Styleable.getCssMetaData.

The CssMetaData of this Styleable.

public final Node
public final String
getId()

Implements javafx.css.Styleable.getId.

The id of this Styleable.

public final EventHandler<ActionEvent>
public final EventHandler<Event>
public final Menu
public final ContextMenu
public ObservableMap<Object, Object>

Returns:

an observable map of properties on this menu item for use primarily by application developers
getProperties
()

Returns an observable map of properties on this menu item for use primarily by application developers.

public final ObservableSet<PseudoClass>
getPseudoClassStates()

Implements javafx.css.Styleable.getPseudoClassStates.

Return the pseudo-class state of this Styleable.

public final String
getStyle()

Implements javafx.css.Styleable.getStyle.

A string representation of the CSS style associated with this specific Node.

public Node
getStyleableNode()

Overrides default javafx.css.Styleable.getStyleableNode.

Returns the Node that represents this Styleable object.

public Styleable

Returns:

getParentMenu(), or getParentPopup() if parentMenu is null
getStyleableParent
()

Implements javafx.css.Styleable.getStyleableParent.

Return the parent of this Styleable, or null if there is no parent.

public ObservableList<String>
getStyleClass()

Implements javafx.css.Styleable.getStyleClass.

A list of String identifiers which can be used to logically group Nodes, specifically for an external style engine.

public final String
public String

Returns:

"MenuItem"
getTypeSelector
()

Implements javafx.css.Styleable.getTypeSelector.

The type of this Styleable that is to be used in selector matching.

public Object

Returns:

the user object that was stored (including null), or null if no object has been set
getUserData
()

Returns the Object that was set by setUserData(Object), or null if no object has been set.

public final ObjectProperty<Node>
public final StringProperty
public final boolean
public final boolean
public final boolean
public final BooleanProperty
public final ObjectProperty<EventHandler<ActionEvent>>
public final ObjectProperty<EventHandler<Event>>
public final ReadOnlyObjectProperty<Menu>
private ReadOnlyObjectWrapper<Menu>
public final ReadOnlyObjectProperty<ContextMenu>
private ReadOnlyObjectWrapper<ContextMenu>
public <E extends Event> void
removeEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter)

Overrides default javafx.event.EventTarget.removeEventFilter.

Unregisters a previously registered event filter from this target.

public <E extends Event> void
removeEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler)

Overrides default javafx.event.EventTarget.removeEventHandler.

Unregisters a previously registered event handler from this target.

public final void
public final void
setDisable(boolean value)

public final void
public final void
setId(String value)

public final void
setMnemonicParsing(boolean value)

public final void
public final void
protected final void
protected final void
public final void
public final void
setText(String value)

public void
setUserData(Object
the user object to be stored
value
)

Stores a user object that contains data at their discretion.

public final void
setVisible(boolean value)

public final StringProperty
public final StringProperty
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

public final BooleanProperty
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

acceleratorback to summary
private ObjectProperty<KeyCombination> accelerator

The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.

DEFAULT_STYLE_CLASSback to summary
private static final String DEFAULT_STYLE_CLASS
disableback to summary
private BooleanProperty disable

Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.

eventHandlerManagerback to summary
pack-priv final EventHandlerManager eventHandlerManager
graphicback to summary
private ObjectProperty<Node> graphic

An optional graphic for the MenuItem. This will normally be an javafx.scene.image.ImageView node, but there is no requirement for this to be the case.

idback to summary
private StringProperty id

The id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.

mnemonicParsingback to summary
private BooleanProperty mnemonicParsing

MnemonicParsing property to enable/disable text parsing. If this is set to true, then the MenuItem text will be parsed to see if it contains the mnemonic parsing character '_'. When a mnemonic is detected the key combination will be determined based on the succeeding character, and the mnemonic added.

The default value for MenuItem is true.

onActionback to summary
private ObjectProperty<EventHandler<ActionEvent>> onAction

The action, which is invoked whenever the MenuItem is fired. This may be due to the user clicking on the button with the mouse, or by a touch event, or by a key press, or if the developer programmatically invokes the fire() method.

onMenuValidationback to summary
private ObjectProperty<EventHandler<Event>> onMenuValidation

The event handler that is associated with invocation of an accelerator for a MenuItem. This can happen when a key sequence for an accelerator is pressed. The event handler is also invoked when onShowing event handler is called.

Since
JavaFX 2.2
parentMenuback to summary
private ReadOnlyObjectWrapper<Menu> parentMenu

This is the Menu in which this MenuItem exists. It is possible for an instance of this class to not have a parentMenu - this means that this instance is either:

  • Not yet associated with its parentMenu.
  • A 'root' Menu (i.e. it is a context menu, attached directly to a MenuBar, MenuButton, or any of the other controls that use Menu internally.
parentPopupback to summary
private ReadOnlyObjectWrapper<ContextMenu> parentPopup

This is the ContextMenu in which this MenuItem exists.

propertiesback to summary
private ObservableMap<Object, Object> properties
styleback to summary
private StringProperty style

A string representation of the CSS style associated with this specific MenuItem. This is analogous to the "style" attribute of an HTML element. Note that, like the HTML style attribute, this variable contains style properties and values and not the selector portion of a style rule.

styleClassback to summary
private final ObservableList<String> styleClass
textback to summary
private StringProperty text

The text to display in the MenuItem.

userDataback to summary
private Object userData
visibleback to summary
private BooleanProperty visible

Specifies whether this MenuItem should be rendered as part of the scene graph.

Constructor Detail

Method Detail

acceleratorPropertyback to summary
public final ObjectProperty<KeyCombination> acceleratorProperty()
addEventFilterback to summary
public <E extends Event> void addEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter)

Overrides default javafx.event.EventTarget.addEventFilter.

Doc from javafx.event.EventTarget.addEventFilter.

Registers an event filter for this target.

The filter is called when the target receives an Event of the specified type during the capturing phase of event delivery.

Annotations
@Override
addEventHandlerback to summary
public <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler)

Overrides default javafx.event.EventTarget.addEventHandler.

Doc from javafx.event.EventTarget.addEventHandler.

Registers an event handler for this target.

The handler is called when the target receives an Event of the specified type during the bubbling phase of event delivery.

Annotations
@Override
buildEventDispatchChainback to summary
public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)

Implements javafx.event.EventTarget.buildEventDispatchChain.

Doc from javafx.event.EventTarget.buildEventDispatchChain.

Construct an event dispatch chain for this target. The event dispatch chain contains event dispatchers which might be interested in processing of events targeted at this EventTarget. This event target is not automatically added to the chain, so if it wants to process events, it needs to add an EventDispatcher for itself to the chain.

In the case the event target is part of some hierarchy, the chain for it is usually built from event dispatchers collected from the root of the hierarchy to the event target.

The event dispatch chain is constructed by modifications to the provided initial event dispatch chain. The returned chain should have the initial chain at its end so the dispatchers should be prepended to the initial chain.

The caller shouldn't assume that the initial chain remains unchanged nor that the returned value will reference a different chain.

Annotations
@Override
disablePropertyback to summary
public final BooleanProperty disableProperty()
fireback to summary
public void fire()

Fires a new ActionEvent.

getAcceleratorback to summary
public final KeyCombination getAccelerator()
getCssMetaDataback to summary
public List<CssMetaData<? extends Styleable, ?>> getCssMetaData()

Implements javafx.css.Styleable.getCssMetaData.

Doc from javafx.css.Styleable.getCssMetaData.

The CssMetaData of this Styleable. This may be returned as an unmodifiable list.

Annotations
@Override
getGraphicback to summary
public final Node getGraphic()
getIdback to summary
public final String getId()

Implements javafx.css.Styleable.getId.

Doc from javafx.css.Styleable.getId.

The id of this Styleable. This simple string identifier is useful for finding a specific Node within the scene graph. While the id of a Node should be unique within the scene graph, this uniqueness is not enforced. This is analogous to the "id" attribute on an HTML element (CSS ID Specification).

For example, if a Node is given the id of "myId", then the lookup method can be used to find this node as follows: scene.lookup("#myId");.

Annotations
@Override
getOnActionback to summary
public final EventHandler<ActionEvent> getOnAction()
getOnMenuValidationback to summary
public final EventHandler<Event> getOnMenuValidation()
getParentMenuback to summary
public final Menu getParentMenu()
getParentPopupback to summary
public final ContextMenu getParentPopup()
getPropertiesback to summary
public ObservableMap<Object, Object> getProperties()

Returns an observable map of properties on this menu item for use primarily by application developers.

Returns:ObservableMap<Object, Object>

an observable map of properties on this menu item for use primarily by application developers

getPseudoClassStatesback to summary
public final ObservableSet<PseudoClass> getPseudoClassStates()

Implements javafx.css.Styleable.getPseudoClassStates.

Doc from javafx.css.Styleable.getPseudoClassStates.

Return the pseudo-class state of this Styleable. CSS assumes this set is read-only.

Annotations
@Override
Since
JavaFX 8.0
getStyleback to summary
public final String getStyle()

Implements javafx.css.Styleable.getStyle.

Doc from javafx.css.Styleable.getStyle.

A string representation of the CSS style associated with this specific Node. This is analogous to the "style" attribute of an HTML element. Note that, like the HTML style attribute, this variable contains style properties and values and not the selector portion of a style rule.

Annotations
@Override
getStyleableNodeback to summary
public Node getStyleableNode()

Overrides default javafx.css.Styleable.getStyleableNode.

Doc from javafx.css.Styleable.getStyleableNode.

Returns the Node that represents this Styleable object. This method should be overridden in cases where the Styleable is not itself a Node, so that it may optionally return the relevant root node representation of itself. By default this method returns null, which can mean that either the Styleable itself is a Node, or if that is not the case, that the Styleable does not have a node representation available at the time of request.

Annotations
@Override
getStyleableParentback to summary
public Styleable getStyleableParent()

Implements javafx.css.Styleable.getStyleableParent.

Doc from javafx.css.Styleable.getStyleableParent.

Return the parent of this Styleable, or null if there is no parent.

Returns:Styleable

getParentMenu(), or getParentPopup() if parentMenu is null

Annotations
@Override
Since
JavaFX 8.0
getStyleClassback to summary
public ObservableList<String> getStyleClass()

Implements javafx.css.Styleable.getStyleClass.

Doc from javafx.css.Styleable.getStyleClass.

A list of String identifiers which can be used to logically group Nodes, specifically for an external style engine. This variable is analogous to the "class" attribute on an HTML element and, as such, each element of the list is a style class to which this Node belongs.

Annotations
@Override
getTextback to summary
public final String getText()
getTypeSelectorback to summary
public String getTypeSelector()

Implements javafx.css.Styleable.getTypeSelector.

Doc from javafx.css.Styleable.getTypeSelector.

The type of this Styleable that is to be used in selector matching. This is analogous to an "element" in HTML. (CSS Type Selector).

Returns:String

"MenuItem"

Annotations
@Override
Since
JavaFX 8.0
getUserDataback to summary
public Object getUserData()

Returns the Object that was set by setUserData(Object), or null if no object has been set.

Returns:Object

the user object that was stored (including null), or null if no object has been set

graphicPropertyback to summary
public final ObjectProperty<Node> graphicProperty()
idPropertyback to summary
public final StringProperty idProperty()
isDisableback to summary
public final boolean isDisable()
isMnemonicParsingback to summary
public final boolean isMnemonicParsing()
isVisibleback to summary
public final boolean isVisible()
mnemonicParsingPropertyback to summary
public final BooleanProperty mnemonicParsingProperty()
onActionPropertyback to summary
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty()
onMenuValidationPropertyback to summary
public final ObjectProperty<EventHandler<Event>> onMenuValidationProperty()
parentMenuPropertyback to summary
public final ReadOnlyObjectProperty<Menu> parentMenuProperty()
parentMenuPropertyImplback to summary
private ReadOnlyObjectWrapper<Menu> parentMenuPropertyImpl()
parentPopupPropertyback to summary
public final ReadOnlyObjectProperty<ContextMenu> parentPopupProperty()
parentPopupPropertyImplback to summary
private ReadOnlyObjectWrapper<ContextMenu> parentPopupPropertyImpl()
removeEventFilterback to summary
public <E extends Event> void removeEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter)

Overrides default javafx.event.EventTarget.removeEventFilter.

Doc from javafx.event.EventTarget.removeEventFilter.

Unregisters a previously registered event filter from this target.

Since it is possible to register a single EventHandler instance for different event types, the caller needs to specify the event type from which the filter should be unregistered.

Annotations
@Override
removeEventHandlerback to summary
public <E extends Event> void removeEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler)

Overrides default javafx.event.EventTarget.removeEventHandler.

Doc from javafx.event.EventTarget.removeEventHandler.

Unregisters a previously registered event handler from this target.

Since it is possible to register a single EventHandler instance for different event types, the caller needs to specify the event type from which the handler should be unregistered.

Annotations
@Override
setAcceleratorback to summary
public final void setAccelerator(KeyCombination value)
setDisableback to summary
public final void setDisable(boolean value)
setGraphicback to summary
public final void setGraphic(Node value)
setIdback to summary
public final void setId(String value)
setMnemonicParsingback to summary
public final void setMnemonicParsing(boolean value)
setOnActionback to summary
public final void setOnAction(EventHandler<ActionEvent> value)
setOnMenuValidationback to summary
public final void setOnMenuValidation(EventHandler<Event> value)
setParentMenuback to summary
protected final void setParentMenu(Menu value)
setParentPopupback to summary
protected final void setParentPopup(ContextMenu value)
setStyleback to summary
public final void setStyle(String value)
setTextback to summary
public final void setText(String value)
setUserDataback to summary
public void setUserData(Object value)

Stores a user object that contains data at their discretion. It can later be retrieved by calling getUserData().

Parameters
value:Object

the user object to be stored

setVisibleback to summary
public final void setVisible(boolean value)
stylePropertyback to summary
public final StringProperty styleProperty()
textPropertyback to summary
public final StringProperty textProperty()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Annotations
@Override
visiblePropertyback to summary
public final BooleanProperty visibleProperty()