Class MenuItem
- 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:
Property Summary
| Type | Property | Description |
|---|---|---|
final ObjectProperty<KeyCombination> |
accelerator |
The accelerator property enables accessing the associated action in one keystroke. |
final BooleanProperty |
disable |
Sets the individual disabled state of this MenuItem. |
final ObjectProperty<Node> |
graphic |
An optional graphic for the MenuItem. |
final StringProperty |
id |
The id of this MenuItem. |
final BooleanProperty |
mnemonicParsing |
MnemonicParsing property to enable/disable text parsing. |
final ObjectProperty<EventHandler<ActionEvent>> |
onAction |
The action, which is invoked whenever the MenuItem is fired. |
final ObjectProperty<EventHandler<Event>> |
onMenuValidation |
The event handler that is associated with invocation of an accelerator for a MenuItem. |
final ReadOnlyObjectProperty<Menu> |
parentMenu |
This is the Menu in which this MenuItem exists. |
final ReadOnlyObjectProperty<ContextMenu> |
parentPopup |
This is the ContextMenu in which this MenuItem exists. |
final StringProperty |
style |
A string representation of the CSS style associated with this specific MenuItem. |
final StringProperty |
text |
The text to display in the MenuItem. |
final BooleanProperty |
visible |
Specifies whether this MenuItem should be rendered as part of the scene graph. |
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
static final EventType<Event> |
MENU_VALIDATION_EVENT |
Called when a accelerator for the Menuitem is invoked |
Constructor Summary
| Constructor | Description |
|---|---|
MenuItem() |
Constructs a MenuItem with no display text. |
MenuItem |
Constructs a MenuItem and sets the display text with the specified text |
MenuItem |
Constructor s MenuItem and sets the display text with the specified text and sets the graphic Node to the given node. |
Method Summary
| Modifier and Type | Method | Description |
|---|---|---|
final ObjectProperty<KeyCombination> |
acceleratorProperty() |
The accelerator property enables accessing the associated action in one keystroke. |
EventDispatchChain |
buildEventDispatchChain |
Construct an event dispatch chain for this target. |
final BooleanProperty |
disableProperty() |
Sets the individual disabled state of this MenuItem. |
void |
fire() |
Fires a new ActionEvent. |
final KeyCombination |
getAccelerator() |
Gets the value of the accelerator property. |
List<CssMetaData<? extends Styleable, |
getCssMetaData() |
The CssMetaData of this Styleable. |
final Node |
getGraphic() |
Gets the value of the graphic property. |
final String |
getId() |
Gets the value of the id property. |
final EventHandler<ActionEvent> |
getOnAction() |
Gets the value of the onAction property. |
final EventHandler<Event> |
getOnMenuValidation() |
Gets the value of the onMenuValidation property. |
final Menu |
getParentMenu() |
Gets the value of the parentMenu property. |
final ContextMenu |
getParentPopup() |
Gets the value of the parentPopup property. |
ObservableMap<Object, |
getProperties() |
Returns an observable map of properties on this menu item for use primarily by application developers. |
final ObservableSet<PseudoClass> |
getPseudoClassStates() |
Return the pseudo-class state of this Styleable. |
final String |
getStyle() |
Gets the value of the style property. |
Styleable |
getStyleableParent() |
Return the parent of this Styleable, or null if there is no parent. |
ObservableList<String> |
getStyleClass() |
A list of String identifiers which can be used to logically group Nodes, specifically for an external style engine. |
final String |
getText() |
Gets the value of the text property. |
String |
getTypeSelector() |
The type of this Styleable that is to be used in selector matching. |
Object |
getUserData() |
|
final ObjectProperty<Node> |
graphicProperty() |
An optional graphic for the MenuItem. |
final StringProperty |
idProperty() |
The id of this MenuItem. |
final boolean |
isDisable() |
Gets the value of the disable property. |
final boolean |
isMnemonicParsing() |
Gets the value of the mnemonicParsing property. |
final boolean |
isVisible() |
Gets the value of the visible property. |
final BooleanProperty |
mnemonicParsingProperty() |
MnemonicParsing property to enable/disable text parsing. |
final ObjectProperty<EventHandler<ActionEvent>> |
onActionProperty() |
The action, which is invoked whenever the MenuItem is fired. |
final ObjectProperty<EventHandler<Event>> |
onMenuValidationProperty() |
The event handler that is associated with invocation of an accelerator for a MenuItem. |
final ReadOnlyObjectProperty<Menu> |
parentMenuProperty() |
This is the Menu in which this MenuItem exists. |
final ReadOnlyObjectProperty<ContextMenu> |
parentPopupProperty() |
This is the ContextMenu in which this MenuItem exists. |
final void |
setAccelerator |
Sets the value of the accelerator property. |
final void |
setDisable |
Sets the value of the disable property. |
final void |
setGraphic |
Sets the value of the graphic property. |
final void |
setId |
Sets the value of the id property. |
final void |
setMnemonicParsing |
Sets the value of the mnemonicParsing property. |
final void |
setOnAction |
Sets the value of the onAction property. |
final void |
setOnMenuValidation |
Sets the value of the onMenuValidation property. |
protected final void |
setParentMenu |
Sets the value of the parentMenu property. |
protected final void |
setParentPopup |
Sets the value of the parentPopup property. |
final void |
setStyle |
Sets the value of the style property. |
final void |
setText |
Sets the value of the text property. |
void |
setUserData |
Stores a user object that contains data at their discretion. |
final void |
setVisible |
Sets the value of the visible property. |
final StringProperty |
styleProperty() |
A string representation of the CSS style associated with this specific MenuItem. |
final StringProperty |
textProperty() |
The text to display in the MenuItem. |
final BooleanProperty |
visibleProperty() |
Specifies whether this MenuItem should be rendered as part of the scene graph. |
Methods declared in class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods declared in interface javafx.event.EventTarget
addEventFilter, addEventHandler, removeEventFilter, removeEventHandler
Methods declared in interface javafx.css.Styleable
getStyleableNode
Property Details
id
public final StringProperty idProperty
- See Also:
style
public final StringProperty styleProperty
- See Also:
parentMenu
public final ReadOnlyObjectProperty<Menu> parentMenuProperty
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 aMenuBar,MenuButton, or any of the other controls that useMenuinternally.
parentPopup
public final ReadOnlyObjectProperty<ContextMenu> parentPopupProperty
ContextMenu in which this MenuItem exists.text
public final StringProperty textProperty
MenuItem.- See Also:
graphic
public final ObjectProperty<Node> graphicProperty
MenuItem. This will normally be an ImageView node, but there is no requirement for this to be the case.- See Also:
onAction
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty
fire() method.onMenuValidation
public final ObjectProperty<EventHandler<Event>> onMenuValidationProperty
- Since:
- JavaFX 2.2
- See Also:
disable
public final BooleanProperty disableProperty
- See Also:
visible
public final BooleanProperty visibleProperty
- See Also:
accelerator
public final ObjectProperty<KeyCombination> acceleratorProperty
mnemonicParsing
public final BooleanProperty mnemonicParsingProperty
The default value for MenuItem is true.
Field Details
MENU_VALIDATION_EVENT
public static final EventType<Event> MENU_VALIDATION_EVENT
Called when a accelerator for the Menuitem is invoked
- Since:
- JavaFX 2.2
Constructor Details
MenuItem
public MenuItem()
MenuItem
public MenuItem(String text)
- Parameters:
-
text- the display text - See Also:
MenuItem
public MenuItem(String text, Node graphic)
Node to the given node.- Parameters:
-
text- the display text -
graphic- the graphic node - See Also:
Method Details
setId
public final void setId(String value)
id property.- 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 theidproperty - See Also:
getId
public final String getId()
id property.- Specified by:
-
getIdin interfaceStyleable - Property description:
- The id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.
- Returns:
- the value of the
idproperty - See Also:
idProperty
public final StringProperty idProperty()
- Returns:
- the
idproperty - See Also:
setStyle
public final void setStyle(String value)
style property.- 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 thestyleproperty - See Also:
getStyle
public final String getStyle()
style property.- Specified by:
-
getStylein interfaceStyleable - 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:
styleProperty
public final StringProperty styleProperty()
- Returns:
- the
styleproperty - See Also:
setParentMenu
protected final void setParentMenu(Menu value)
parentMenu property.- 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 theparentMenuproperty - See Also:
getParentMenu
public final Menu getParentMenu()
parentMenu property.- 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:
parentMenuProperty
public final ReadOnlyObjectProperty<Menu> parentMenuProperty()
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 aMenuBar,MenuButton, or any of the other controls that useMenuinternally.
- Returns:
- the
parentMenuproperty - See Also:
setParentPopup
protected final void setParentPopup(ContextMenu value)
parentPopup property.- Property description:
- This is the
ContextMenuin which thisMenuItemexists. - Parameters:
-
value- the value for theparentPopupproperty - See Also:
getParentPopup
public final ContextMenu getParentPopup()
parentPopup property.- Property description:
- This is the
ContextMenuin which thisMenuItemexists. - Returns:
- the value of the
parentPopupproperty - See Also:
parentPopupProperty
public final ReadOnlyObjectProperty<ContextMenu> parentPopupProperty()
ContextMenu in which this MenuItem exists.- Returns:
- the
parentPopupproperty - See Also:
setText
public final void setText(String value)
text property.- Property description:
- The text to display in the
MenuItem. - Parameters:
-
value- the value for thetextproperty - See Also:
getText
public final String getText()
text property.- Property description:
- The text to display in the
MenuItem. - Returns:
- the value of the
textproperty - See Also:
textProperty
public final StringProperty textProperty()
MenuItem.- Returns:
- the
textproperty - See Also:
setGraphic
public final void setGraphic(Node value)
graphic property.- 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 thegraphicproperty - See Also:
getGraphic
public final Node getGraphic()
graphic property.- 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:
graphicProperty
public final ObjectProperty<Node> graphicProperty()
MenuItem. This will normally be an ImageView node, but there is no requirement for this to be the case.- Returns:
- the
graphicproperty - See Also:
setOnAction
public final void setOnAction(EventHandler<ActionEvent> value)
onAction property.- 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. - Parameters:
-
value- the value for theonActionproperty - See Also:
getOnAction
public final EventHandler<ActionEvent> getOnAction()
onAction property.- 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. - Returns:
- the value of the
onActionproperty - See Also:
onActionProperty
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty()
fire() method.- Returns:
- the
onActionproperty - See Also:
setOnMenuValidation
public final void setOnMenuValidation(EventHandler<Event> value)
onMenuValidation property.- 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 theonMenuValidationproperty - Since:
- JavaFX 2.2
- See Also:
getOnMenuValidation
public final EventHandler<Event> getOnMenuValidation()
onMenuValidation property.- 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:
onMenuValidationProperty
public final ObjectProperty<EventHandler<Event>> onMenuValidationProperty()
- Returns:
- the
onMenuValidationproperty - Since:
- JavaFX 2.2
- See Also:
setDisable
public final void setDisable(boolean value)
disable property.- 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 thedisableproperty - See Also:
isDisable
public final boolean isDisable()
disable property.- 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:
disableProperty
public final BooleanProperty disableProperty()
- Returns:
- the
disableproperty - See Also:
setVisible
public final void setVisible(boolean value)
visible property.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
- Parameters:
-
value- the value for thevisibleproperty - See Also:
isVisible
public final boolean isVisible()
visible property.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
- Returns:
- the value of the
visibleproperty - See Also:
visibleProperty
public final BooleanProperty visibleProperty()
- Returns:
- the
visibleproperty - See Also:
setAccelerator
public final void setAccelerator(KeyCombination value)
accelerator property.- 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 theacceleratorproperty - See Also:
getAccelerator
public final KeyCombination getAccelerator()
accelerator property.- 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:
acceleratorProperty
public final ObjectProperty<KeyCombination> acceleratorProperty()
- Returns:
- the
acceleratorproperty - See Also:
setMnemonicParsing
public final void setMnemonicParsing(boolean value)
mnemonicParsing property.- 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 themnemonicParsingproperty - See Also:
isMnemonicParsing
public final boolean isMnemonicParsing()
mnemonicParsing property.- 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:
mnemonicParsingProperty
public final BooleanProperty mnemonicParsingProperty()
The default value for MenuItem is true.
- Returns:
- the
mnemonicParsingproperty - See Also:
getStyleClass
public ObservableList<String> getStyleClass()
Styleable- Specified by:
-
getStyleClassin interfaceStyleable - Returns:
- a list of String identifiers which can be used to logically group Nodes, specifically for an external style engine
- See Also:
fire
public void fire()
buildEventDispatchChain
public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)
EventTargetEventTarget. 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.
- Specified by:
-
buildEventDispatchChainin interfaceEventTarget - Parameters:
-
tail- the initial chain to build from - Returns:
- the resulting event dispatch chain for this target
getUserData
public Object getUserData()
- Returns:
- the user object that was stored (including
null), ornullif no object has been set
setUserData
public void setUserData(Object value)
getUserData().- Parameters:
-
value- the user object to be stored
getProperties
public ObservableMap<Object,Object> getProperties()
- Returns:
- an observable map of properties on this menu item for use primarily by application developers
getTypeSelector
public String getTypeSelector()
Styleable that is to be used in selector matching. This is analogous to an "element" in HTML. (CSS Type Selector).- Specified by:
-
getTypeSelectorin interfaceStyleable - Returns:
- "MenuItem"
- Since:
- JavaFX 8.0
getStyleableParent
public Styleable getStyleableParent()
- Specified by:
-
getStyleableParentin interfaceStyleable - Returns:
-
getParentMenu(), orgetParentPopup()ifparentMenuis null - Since:
- JavaFX 8.0
getPseudoClassStates
public final ObservableSet<PseudoClass> getPseudoClassStates()
- Specified by:
-
getPseudoClassStatesin interfaceStyleable - Returns:
- the pseudo-class state
- Since:
- JavaFX 8.0
getCssMetaData
public List<CssMetaData<? extends Styleable,?>> getCssMetaData()
Styleable- Specified by:
-
getCssMetaDatain interfaceStyleable - Returns:
- the CssMetaData
© 2008, 2025, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from JavaFX API Documentation.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Java, JavaFX and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.