- All Implemented Interfaces:
- Styleable,- EventTarget
- Direct Known Subclasses:
- CheckMenuItem,- CustomMenuItem,- Menu,- RadioMenuItem
 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:
- 
Property SummaryPropertiesTypePropertyDescriptionfinal 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 BooleanPropertyMnemonicParsing property to enable/disable text parsing.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 SummaryFieldsModifier and TypeFieldDescriptionCalled when a accelerator for the Menuitem is invoked
- 
Constructor SummaryConstructors
- 
Method SummaryModifier 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 BooleanPropertyMnemonicParsing property to enable/disable text parsing.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 java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods declared in interface javafx.event.EventTargetaddEventFilter, addEventHandler, removeEventFilter, removeEventHandlerMethods declared in interface javafx.css.StyleablegetStyleableNode
- 
Property Details- 
idThe id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.- See Also:
 
- 
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:
 
- 
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:
 
- Not yet associated with its 
- 
parentPopupThis is theContextMenuin which thisMenuItemexists.- See Also:
 
- 
textThe text to display in theMenuItem.- See Also:
 
- 
graphicAn optional graphic for theMenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.- See Also:
 
- 
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 programmatically invokes thefire()method.- See Also:
 
- 
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:
 
- 
disableSets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.- See Also:
 
- 
visibleSpecifies whether this MenuItem should be rendered as part of the scene graph.- See Also:
 
- 
acceleratorThe accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.- See Also:
 
- 
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:
 
 
- 
- 
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:
 
- 
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:
 
 
- 
- 
Method Details- 
setIdSets the value of theidproperty.- Property description:
- The id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.
- Parameters:
- value- the value for the- idproperty
- See Also:
 
- 
getIdGets the value of theidproperty.
- 
idPropertyThe id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.- Returns:
- the idproperty
- See Also:
 
- 
setStyleSets the value of thestyleproperty.- 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.
- Parameters:
- value- the value for the- styleproperty
- See Also:
 
- 
getStyleGets the value of thestyleproperty.- 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:
- the value of the styleproperty
- See Also:
 
- 
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.- Returns:
- the styleproperty
- See Also:
 
- 
setParentMenuSets the value of theparentMenuproperty.- 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 
- Parameters:
- value- the value for the- parentMenuproperty
- See Also:
 
- 
getParentMenuGets the value of theparentMenuproperty.- 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 
- Returns:
- the value of the parentMenuproperty
- See Also:
 
- 
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.
 - Returns:
- the parentMenuproperty
- See Also:
 
- Not yet associated with its 
- 
setParentPopupSets the value of theparentPopupproperty.- Property description:
- This is the ContextMenuin which thisMenuItemexists.
- Parameters:
- value- the value for the- parentPopupproperty
- See Also:
 
- 
getParentPopupGets the value of theparentPopupproperty.- Property description:
- This is the ContextMenuin which thisMenuItemexists.
- Returns:
- the value of the parentPopupproperty
- See Also:
 
- 
parentPopupPropertyThis is theContextMenuin which thisMenuItemexists.- Returns:
- the parentPopupproperty
- See Also:
 
- 
setTextSets the value of thetextproperty.- Property description:
- The text to display in the MenuItem.
- Parameters:
- value- the value for the- textproperty
- See Also:
 
- 
getTextGets the value of thetextproperty.- Property description:
- The text to display in the MenuItem.
- Returns:
- the value of the textproperty
- See Also:
 
- 
textPropertyThe text to display in theMenuItem.- Returns:
- the textproperty
- See Also:
 
- 
setGraphicSets the value of thegraphicproperty.- Property description:
- An optional graphic for the MenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.
- Parameters:
- value- the value for the- graphicproperty
- See Also:
 
- 
getGraphicGets the value of thegraphicproperty.- Property description:
- An optional graphic for the MenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.
- Returns:
- the value of the graphicproperty
- See Also:
 
- 
graphicPropertyAn optional graphic for theMenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.- Returns:
- the graphicproperty
- See Also:
 
- 
setOnActionSets the value of theonActionproperty.- 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 programmatically
 invokes the fire()method.
- Parameters:
- value- the value for the- onActionproperty
- See Also:
 
- 
getOnActionGets the value of theonActionproperty.- 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 programmatically
 invokes the fire()method.
- Returns:
- the value of the onActionproperty
- See Also:
 
- 
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 programmatically invokes thefire()method.- Returns:
- the onActionproperty
- See Also:
 
- 
setOnMenuValidationSets the value of theonMenuValidationproperty.- 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.
- Parameters:
- value- the value for the- onMenuValidationproperty
- Since:
- JavaFX 2.2
- See Also:
 
- 
getOnMenuValidationGets the value of theonMenuValidationproperty.- 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.
- Returns:
- the value of the onMenuValidationproperty
- Since:
- JavaFX 2.2
- See Also:
 
- 
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.- Returns:
- the onMenuValidationproperty
- Since:
- JavaFX 2.2
- See Also:
 
- 
setDisablepublic final void setDisable(boolean value) Sets the value of thedisableproperty.- Property description:
- Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.
- Parameters:
- value- the value for the- disableproperty
- See Also:
 
- 
isDisablepublic final boolean isDisable()Gets the value of thedisableproperty.- Property description:
- Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.
- Returns:
- the value of the disableproperty
- See Also:
 
- 
disablePropertySets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.- Returns:
- the disableproperty
- See Also:
 
- 
setVisiblepublic final void setVisible(boolean value) Sets the value of thevisibleproperty.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
- Parameters:
- value- the value for the- visibleproperty
- See Also:
 
- 
isVisiblepublic final boolean isVisible()Gets the value of thevisibleproperty.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
- Returns:
- the value of the visibleproperty
- See Also:
 
- 
visiblePropertySpecifies whether this MenuItem should be rendered as part of the scene graph.- Returns:
- the visibleproperty
- See Also:
 
- 
setAcceleratorSets the value of theacceleratorproperty.- Property description:
- The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.
- Parameters:
- value- the value for the- acceleratorproperty
- See Also:
 
- 
getAcceleratorGets the value of theacceleratorproperty.- Property description:
- The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.
- Returns:
- the value of the acceleratorproperty
- See Also:
 
- 
acceleratorPropertyThe accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.- Returns:
- the acceleratorproperty
- See Also:
 
- 
setMnemonicParsingpublic final void setMnemonicParsing(boolean value) Sets the value of themnemonicParsingproperty.- 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. 
- Parameters:
- value- the value for the- mnemonicParsingproperty
- See Also:
 
- 
isMnemonicParsingpublic final boolean isMnemonicParsing()Gets the value of themnemonicParsingproperty.- 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. 
- Returns:
- the value of the mnemonicParsingproperty
- See Also:
 
- 
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. - Returns:
- the mnemonicParsingproperty
- See Also:
 
- 
getStyleClassDescription copied from interface:StyleableA 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.- 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:
 
- 
firepublic void fire()Fires a new ActionEvent.
- 
buildEventDispatchChainDescription copied from interface:EventTargetConstruct 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
 
- 
getUserData- Returns:
- the user object that was stored (including null), ornullif no object has been set
 
- 
setUserDataStores a user object that contains data at their discretion. It can later be retrieved by callinggetUserData().- Parameters:
- value- the user object to be stored
 
- 
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
 
 
-