- All Implemented Interfaces:
- Styleable,- EventTarget
- Direct Known Subclasses:
- CheckMenuItem,- CustomMenuItem,- Menu,- RadioMenuItem
@IDProperty("id")
public class MenuItem
extends Object
implements EventTarget, Styleable
 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(javafx.event.EventHandler<javafx.event.ActionEvent>) 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);
- Since:
- JavaFX 2.0
- See Also:
- Menu
- 
Property SummaryProperties Type Property Description ObjectProperty<KeyCombination>acceleratorThe accelerator property enables accessing the associated action in one keystroke.BooleanPropertydisableSets the individual disabled state of this MenuItem.ObjectProperty<Node>graphicAn optional graphic for theMenuItem.StringPropertyidThe id of this MenuItem.BooleanPropertymnemonicParsingMnemonicParsing property to enable/disable text parsing.ObjectProperty<EventHandler<ActionEvent>>onActionThe action, which is invoked whenever the MenuItem is fired.ObjectProperty<EventHandler<Event>>onMenuValidationThe event handler that is associated with invocation of an accelerator for a MenuItem.ReadOnlyObjectProperty<Menu>parentMenuThis is theMenuin which thisMenuItemexists.ReadOnlyObjectProperty<ContextMenu>parentPopupThis is theContextMenuin which thisMenuItemexists.StringPropertystyleA string representation of the CSS style associated with this specific MenuItem.StringPropertytextThe text to display in theMenuItem.BooleanPropertyvisibleSpecifies whether this MenuItem should be rendered as part of the scene graph.
- 
Field SummaryFields Modifier and Type Field Description static EventType<Event>MENU_VALIDATION_EVENTCalled when a accelerator for the Menuitem is invoked
- 
Constructor SummaryConstructors Constructor Description MenuItem()Constructs a MenuItem with no display text.MenuItem(String text)Constructs a MenuItem and sets the display text with the specified textMenuItem(String text, Node graphic)Constructor s MenuItem and sets the display text with the specified text and sets the graphicNodeto the given node.
- 
Method SummaryModifier and Type Method Description ObjectProperty<KeyCombination>acceleratorProperty()The accelerator property enables accessing the associated action in one keystroke.<E extends Event>
 voidaddEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)Registers an event handler to this MenuItem.EventDispatchChainbuildEventDispatchChain(EventDispatchChain tail)Construct an event dispatch chain for this target.BooleanPropertydisableProperty()Sets the individual disabled state of this MenuItem.voidfire()Fires a new ActionEvent.KeyCombinationgetAccelerator()Gets the value of the property accelerator.List<CssMetaData<? extends Styleable,?>>getCssMetaData()The CssMetaData of this Styleable.NodegetGraphic()Gets the value of the property graphic.StringgetId()Gets the value of the property id.EventHandler<ActionEvent>getOnAction()Gets the value of the property onAction.EventHandler<Event>getOnMenuValidation()Gets the value of the property onMenuValidation.MenugetParentMenu()Gets the value of the property parentMenu.ContextMenugetParentPopup()Gets the value of the property parentPopup.ObservableMap<Object,Object>getProperties()Returns an observable map of properties on this menu item for use primarily by application developers.ObservableSet<PseudoClass>getPseudoClassStates()Return the pseudo-class state of this Styleable.StringgetStyle()Gets the value of the property style.NodegetStyleableNode()Returns the Node that represents this Styleable object.StyleablegetStyleableParent()Return the parent of this Styleable, or null if there is no parent.ObservableList<String>getStyleClass()* Public API * *StringgetText()Gets the value of the property text.StringgetTypeSelector()The type of thisStyleablethat is to be used in selector matching.ObjectgetUserData()Returns a previously set Object property, or null if no such property has been set using thesetUserData(java.lang.Object)method.ObjectProperty<Node>graphicProperty()An optional graphic for theMenuItem.StringPropertyidProperty()The id of this MenuItem.booleanisDisable()Gets the value of the property disable.booleanisMnemonicParsing()Gets the value of the property mnemonicParsing.booleanisVisible()Gets the value of the property visible.BooleanPropertymnemonicParsingProperty()MnemonicParsing property to enable/disable text parsing.ObjectProperty<EventHandler<ActionEvent>>onActionProperty()The action, which is invoked whenever the MenuItem is fired.ObjectProperty<EventHandler<Event>>onMenuValidationProperty()The event handler that is associated with invocation of an accelerator for a MenuItem.ReadOnlyObjectProperty<Menu>parentMenuProperty()This is theMenuin which thisMenuItemexists.ReadOnlyObjectProperty<ContextMenu>parentPopupProperty()This is theContextMenuin which thisMenuItemexists.<E extends Event>
 voidremoveEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)Unregisters a previously registered event handler from this MenuItem.voidsetAccelerator(KeyCombination value)Sets the value of the property accelerator.voidsetDisable(boolean value)Sets the value of the property disable.voidsetGraphic(Node value)Sets the value of the property graphic.voidsetId(String value)Sets the value of the property id.voidsetMnemonicParsing(boolean value)Sets the value of the property mnemonicParsing.voidsetOnAction(EventHandler<ActionEvent> value)Sets the value of the property onAction.voidsetOnMenuValidation(EventHandler<Event> value)Sets the value of the property onMenuValidation.protected voidsetParentMenu(Menu value)Sets the value of the property parentMenu.protected voidsetParentPopup(ContextMenu value)Sets the value of the property parentPopup.voidsetStyle(String value)Sets the value of the property style.voidsetText(String value)Sets the value of the property text.voidsetUserData(Object value)Convenience method for setting a single Object property that can be retrieved at a later date.voidsetVisible(boolean value)Sets the value of the property visible.StringPropertystyleProperty()A string representation of the CSS style associated with this specific MenuItem.StringPropertytextProperty()The text to display in theMenuItem.StringtoString()BooleanPropertyvisibleProperty()Specifies whether this MenuItem should be rendered as part of the scene graph.
- 
Property Details- 
idThe id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.- See Also:
- getId(),- setId(String)
 
- 
styleA 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.- See Also:
- getStyle(),- setStyle(String)
 
- 
parentMenuThis is theMenuin which thisMenuItemexists. It is possible for an instance of this class to not have aparentMenu- 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 aMenuBar,MenuButton, or any of the other controls that useMenuinternally.
 - See Also:
- getParentMenu(),- setParentMenu(Menu)
 
- Not yet associated with its 
- 
parentPopupThis is theContextMenuin which thisMenuItemexists.- See Also:
- getParentPopup(),- setParentPopup(ContextMenu)
 
- 
textThe text to display in theMenuItem.- See Also:
- getText(),- setText(String)
 
- 
graphicAn optional graphic for theMenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.- See Also:
- getGraphic(),- setGraphic(Node)
 
- 
onActionThe 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 programatically invokes thefire()method.- See Also:
- getOnAction(),- setOnAction(EventHandler)
 
- 
onMenuValidationThe 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
- See Also:
- getOnMenuValidation(),- setOnMenuValidation(EventHandler)
 
- 
disableSets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.- See Also:
- isDisable(),- setDisable(boolean)
 
- 
visibleSpecifies whether this MenuItem should be rendered as part of the scene graph.- See Also:
- isVisible(),- setVisible(boolean)
 
- 
acceleratorThe accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.- See Also:
- getAccelerator(),- setAccelerator(KeyCombination)
 
- 
mnemonicParsingMnemonicParsing 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. - See Also:
- isMnemonicParsing(),- setMnemonicParsing(boolean)
 
 
- 
- 
Field Details- 
MENU_VALIDATION_EVENTCalled when a accelerator for the Menuitem is invoked - Since:
- JavaFX 2.2
 
 
- 
- 
Constructor Details- 
MenuItempublic MenuItem()Constructs a MenuItem with no display text.
- 
MenuItemConstructs a MenuItem and sets the display text with the specified text- Parameters:
- text- the display text
- See Also:
- setText(java.lang.String)
 
- 
MenuItemConstructor s MenuItem and sets the display text with the specified text and sets the graphicNodeto the given node.- Parameters:
- text- the display text
- graphic- the graphic node
- See Also:
- setText(java.lang.String),- setGraphic(javafx.scene.Node)
 
 
- 
- 
Method Details- 
setIdSets the value of the property id.- Property description:
- The id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.
 
- 
getIdGets the value of the property id.
- 
idPropertyThe id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.- See Also:
- getId(),- setId(String)
 
- 
setStyleSets the value of the property style.- Property description:
- 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.
 
- 
getStyleGets the value of the property style.- Specified by:
- getStylein interface- Styleable
- Property description:
- 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.
- Returns:
- a string representation of the CSS style associated with this
 specific Node
 
- 
stylePropertyA 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.- See Also:
- getStyle(),- setStyle(String)
 
- 
setParentMenuSets the value of the property parentMenu.- Property description:
- This is the Menuin which thisMenuItemexists. It is possible for an instance of this class to not have aparentMenu- 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 aMenuBar,MenuButton, or any of the other controls that useMenuinternally.
 
- Not yet associated with its 
 
- 
getParentMenuGets the value of the property parentMenu.- Property description:
- This is the Menuin which thisMenuItemexists. It is possible for an instance of this class to not have aparentMenu- 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 aMenuBar,MenuButton, or any of the other controls that useMenuinternally.
 
- Not yet associated with its 
 
- 
parentMenuPropertyThis is theMenuin which thisMenuItemexists. It is possible for an instance of this class to not have aparentMenu- 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 aMenuBar,MenuButton, or any of the other controls that useMenuinternally.
 - See Also:
- getParentMenu(),- setParentMenu(Menu)
 
- Not yet associated with its 
- 
setParentPopupSets the value of the property parentPopup.- Property description:
- This is the ContextMenuin which thisMenuItemexists.
 
- 
getParentPopupGets the value of the property parentPopup.- Property description:
- This is the ContextMenuin which thisMenuItemexists.
 
- 
parentPopupPropertyThis is theContextMenuin which thisMenuItemexists.- See Also:
- getParentPopup(),- setParentPopup(ContextMenu)
 
- 
setTextSets the value of the property text.- Property description:
- The text to display in the MenuItem.
 
- 
getTextGets the value of the property text.- Property description:
- The text to display in the MenuItem.
 
- 
textPropertyThe text to display in theMenuItem.- See Also:
- getText(),- setText(String)
 
- 
setGraphicSets the value of the property graphic.- Property description:
- An optional graphic for the MenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.
 
- 
getGraphicGets the value of the property graphic.- Property description:
- An optional graphic for the MenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.
 
- 
graphicPropertyAn optional graphic for theMenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.- See Also:
- getGraphic(),- setGraphic(Node)
 
- 
setOnActionSets the value of the property onAction.- Property description:
- 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 programatically
 invokes the fire()method.
 
- 
getOnActionGets the value of the property onAction.- Property description:
- 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 programatically
 invokes the fire()method.
 
- 
onActionPropertyThe 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 programatically invokes thefire()method.- See Also:
- getOnAction(),- setOnAction(EventHandler)
 
- 
setOnMenuValidationSets the value of the property onMenuValidation.- Property description:
- 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
 
- 
getOnMenuValidationGets the value of the property onMenuValidation.- Property description:
- 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
 
- 
onMenuValidationPropertyThe 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
- See Also:
- getOnMenuValidation(),- setOnMenuValidation(EventHandler)
 
- 
setDisablepublic final void setDisable(boolean value)Sets the value of the property disable.- Property description:
- Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.
 
- 
isDisablepublic final boolean isDisable()Gets the value of the property disable.- Property description:
- Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.
 
- 
disablePropertySets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.- See Also:
- isDisable(),- setDisable(boolean)
 
- 
setVisiblepublic final void setVisible(boolean value)Sets the value of the property visible.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
 
- 
isVisiblepublic final boolean isVisible()Gets the value of the property visible.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
 
- 
visiblePropertySpecifies whether this MenuItem should be rendered as part of the scene graph.- See Also:
- isVisible(),- setVisible(boolean)
 
- 
setAcceleratorSets the value of the property accelerator.- Property description:
- The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.
 
- 
getAcceleratorGets the value of the property accelerator.- Property description:
- The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.
 
- 
acceleratorPropertyThe accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.- See Also:
- getAccelerator(),- setAccelerator(KeyCombination)
 
- 
setMnemonicParsingpublic final void setMnemonicParsing(boolean value)Sets the value of the property mnemonicParsing.- Property description:
- 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. 
 
- 
isMnemonicParsingpublic final boolean isMnemonicParsing()Gets the value of the property mnemonicParsing.- Property description:
- 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. 
 
- 
mnemonicParsingPropertyMnemonicParsing 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. - See Also:
- isMnemonicParsing(),- setMnemonicParsing(boolean)
 
- 
getStyleClass* Public API * *- Specified by:
- getStyleClassin interface- Styleable
- Returns:
- a list of String identifiers which can be used to logically group Nodes, specifically for an external style engine
- See Also:
- CSS3 class selectors
 
- 
firepublic void fire()Fires a new ActionEvent.
- 
addEventHandlerpublic <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)Registers an event handler to this MenuItem. The handler is called when the menu item receives anEventof the specified type during the bubbling phase of event delivery.- Type Parameters:
- E- the specific event class of the handler
- Parameters:
- eventType- the type of the events to receive by the handler
- eventHandler- the handler to register
- Throws:
- NullPointerException- if the event type or handler is null
 
- 
removeEventHandlerpublic <E extends Event> void removeEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)Unregisters a previously registered event handler from this MenuItem. One handler might have been registered for different event types, so the caller needs to specify the particular event type from which to unregister the handler.- Type Parameters:
- E- the specific event class of the handler
- Parameters:
- eventType- the event type from which to unregister
- eventHandler- the handler to unregister
- Throws:
- NullPointerException- if the event type or handler is null
 
- 
buildEventDispatchChainConstruct an event dispatch chain for this target. The event dispatch chain contains event dispatchers which might be interested in processing of events targeted at thisEventTarget. This event target is not automatically added to the chain, so if it wants to process events, it needs to add anEventDispatcherfor 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. - Specified by:
- buildEventDispatchChainin interface- EventTarget
- Parameters:
- tail- the initial chain to build from
- Returns:
- the resulting event dispatch chain for this target
 
- 
getUserDataReturns a previously set Object property, or null if no such property has been set using thesetUserData(java.lang.Object)method.- Returns:
- The Object that was previously set, or null if no property has been set or if null was set.
 
- 
setUserDataConvenience method for setting a single Object property that can be retrieved at a later date. This is functionally equivalent to calling the getProperties().put(Object key, Object value) method. This can later be retrieved by callingNode.getUserData().- Parameters:
- value- The value to be stored - this can later be retrieved by calling- Node.getUserData().
 
- 
getPropertiesReturns an observable map of properties on this menu item for use primarily by application developers.- Returns:
- an observable map of properties on this menu item for use primarily by application developers
 
- 
getTypeSelectorThe type of thisStyleablethat is to be used in selector matching. This is analogous to an "element" in HTML. (CSS Type Selector).- Specified by:
- getTypeSelectorin interface- Styleable
- Returns:
- "MenuItem"
- Since:
- JavaFX 8.0
 
- 
getStyleableParentReturn the parent of this Styleable, or null if there is no parent.- Specified by:
- getStyleableParentin interface- Styleable
- Returns:
- getParentMenu(), or- getParentPopup()if- parentMenuis null
- Since:
- JavaFX 8.0
 
- 
getPseudoClassStatesReturn the pseudo-class state of this Styleable. CSS assumes this set is read-only.- Specified by:
- getPseudoClassStatesin interface- Styleable
- Returns:
- the pseudo-class state
- Since:
- JavaFX 8.0
 
- 
getCssMetaDataDescription copied from interface:StyleableThe CssMetaData of this Styleable. This may be returned as an unmodifiable list.- Specified by:
- getCssMetaDatain interface- Styleable
- Returns:
- the CssMetaData
 
- 
getStyleableNodeReturns 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.- Specified by:
- getStyleableNodein interface- Styleable
- Returns:
- the Node that represents this Styleable object
 
- 
toString
 
-