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:
Menu
Property Summary
| Type | Property | Description |
|---|---|---|
ObjectProperty<KeyCombination> | accelerator | The accelerator property enables accessing the associated action in one keystroke. |
BooleanProperty | disable | Sets the individual disabled state of this MenuItem. |
ObjectProperty<Node> | graphic | An optional graphic for the MenuItem. |
StringProperty | id | The id of this MenuItem. |
BooleanProperty | mnemonicParsing | MnemonicParsing property to enable/disable text parsing. |
ObjectProperty<EventHandler<ActionEvent>> | onAction | The action, which is invoked whenever the MenuItem is fired. |
ObjectProperty<EventHandler<Event>> | onMenuValidation | The event handler that is associated with invocation of an accelerator for a MenuItem. |
ReadOnlyObjectProperty<Menu> | parentMenu | This is the Menu in which this MenuItem exists. |
ReadOnlyObjectProperty<ContextMenu> | parentPopup | This is the ContextMenu in which this MenuItem exists. |
StringProperty | style | A string representation of the CSS style associated with this specific MenuItem. |
StringProperty | text | The text to display in the MenuItem. |
BooleanProperty | visible | Specifies whether this MenuItem should be rendered as part of the scene graph. |
Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
static 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(String text) | Constructs a MenuItem and sets the display text with the specified text |
MenuItem(String text,
Node graphic) | 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 |
|---|---|---|
ObjectProperty<KeyCombination> | acceleratorProperty() | The accelerator property enables accessing the associated action in one keystroke. |
<E extends Event> | addEventHandler(EventType<E> eventType,
EventHandler<E> eventHandler) | Registers an event handler to this MenuItem. |
BooleanProperty | disableProperty() | Sets the individual disabled state of this MenuItem. |
void | fire() | Fires a new ActionEvent. |
KeyCombination | getAccelerator() | Gets the value of the property accelerator. |
Node | getGraphic() | Gets the value of the property graphic. |
EventHandler<ActionEvent> | getOnAction() | Gets the value of the property onAction. |
EventHandler<Event> | getOnMenuValidation() | Gets the value of the property onMenuValidation. |
Menu | getParentMenu() | Gets the value of the property parentMenu. |
ContextMenu | getParentPopup() | 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. |
Styleable | getStyleableParent() | Return the parent of this Styleable, or null if there is no parent. |
String | getText() | Gets the value of the property text. |
String | getTypeSelector() | The type of this Styleable that is to be used in selector matching. |
Object | getUserData() | |
ObjectProperty<Node> | graphicProperty() | An optional graphic for the MenuItem. |
StringProperty | idProperty() | The id of this MenuItem. |
boolean | isDisable() | Gets the value of the property disable. |
boolean | isMnemonicParsing() | Gets the value of the property mnemonicParsing. |
boolean | isVisible() | Gets the value of the property visible. |
BooleanProperty | mnemonicParsingProperty() | 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 the Menu in which this MenuItem exists. |
ReadOnlyObjectProperty<ContextMenu> | parentPopupProperty() | This is the ContextMenu in which this MenuItem exists. |
<E extends Event> | removeEventHandler(EventType<E> eventType,
EventHandler<E> eventHandler) | Unregisters a previously registered event handler from this MenuItem. |
void | setAccelerator(KeyCombination value) | Sets the value of the property accelerator. |
void | setDisable(boolean value) | Sets the value of the property disable. |
void | setGraphic(Node value) | Sets the value of the property graphic. |
void | setId(String value) | Sets the value of the property id. |
void | setMnemonicParsing(boolean value) | Sets the value of the property mnemonicParsing. |
void | setOnAction(EventHandler<ActionEvent> value) | Sets the value of the property onAction. |
void | setOnMenuValidation(EventHandler<Event> value) | Sets the value of the property onMenuValidation. |
protected void | setParentMenu(Menu value) | Sets the value of the property parentMenu. |
protected void | setParentPopup(ContextMenu value) | Sets the value of the property parentPopup. |
void | setStyle(String value) | Sets the value of the property style. |
void | setText(String value) | Sets the value of the property text. |
void | setUserData(Object value) | Stores a user object that contains data at their discretion. |
void | setVisible(boolean value) | Sets the value of the property visible. |
StringProperty | styleProperty() | A string representation of the CSS style associated with this specific MenuItem. |
StringProperty | textProperty() | The text to display in the MenuItem. |
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
buildEventDispatchChain
Methods declared in interface javafx.css.Styleable
getCssMetaData, getId, getStyle, getStyleableNode, getStyleClass
Property Details
id
public final StringProperty idProperty
- See Also:
-
Styleable.getId(),setId(String)
style
public final StringProperty styleProperty
- See Also:
-
Styleable.getStyle(),setStyle(String)
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.
- See Also:
-
getParentMenu(),setParentMenu(Menu)
parentPopup
public final ReadOnlyObjectProperty<ContextMenu> parentPopupProperty
ContextMenu in which this MenuItem exists.- See Also:
-
getParentPopup(),setParentPopup(ContextMenu)
text
public final StringProperty textProperty
MenuItem.- See Also:
-
getText(),setText(String)
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:
-
getGraphic(),setGraphic(Node)
onAction
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty
fire() method.- See Also:
-
getOnAction(),setOnAction(EventHandler)
onMenuValidation
public final ObjectProperty<EventHandler<Event>> onMenuValidationProperty
- Since:
- JavaFX 2.2
- See Also:
-
getOnMenuValidation(),setOnMenuValidation(EventHandler)
disable
public final BooleanProperty disableProperty
- See Also:
-
isDisable(),setDisable(boolean)
visible
public final BooleanProperty visibleProperty
- See Also:
-
isVisible(),setVisible(boolean)
accelerator
public final ObjectProperty<KeyCombination> acceleratorProperty
- See Also:
-
getAccelerator(),setAccelerator(KeyCombination)
mnemonicParsing
public final BooleanProperty mnemonicParsingProperty
The default value for MenuItem is true.
- See Also:
-
isMnemonicParsing(),setMnemonicParsing(boolean)
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:
setText(java.lang.String)
MenuItem
public MenuItem(String text, Node graphic)
Node to the given node.- Parameters:
-
text- the display text -
graphic- the graphic node - See Also:
-
setText(java.lang.String),setGraphic(javafx.scene.Node)
Method Details
setId
public final void setId(String value)
- Property description:
- The id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.
idProperty
public final StringProperty idProperty()
- See Also:
-
Styleable.getId(),setId(String)
setStyle
public final void setStyle(String value)
- 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.
styleProperty
public final StringProperty styleProperty()
- See Also:
-
Styleable.getStyle(),setStyle(String)
setParentMenu
protected final void setParentMenu(Menu value)
- 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
getParentMenu
public final Menu getParentMenu()
- 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
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.
- See Also:
-
getParentMenu(),setParentMenu(Menu)
setParentPopup
protected final void setParentPopup(ContextMenu value)
- Property description:
- This is the
ContextMenuin which thisMenuItemexists.
getParentPopup
public final ContextMenu getParentPopup()
- Property description:
- This is the
ContextMenuin which thisMenuItemexists.
parentPopupProperty
public final ReadOnlyObjectProperty<ContextMenu> parentPopupProperty()
ContextMenu in which this MenuItem exists.- See Also:
-
getParentPopup(),setParentPopup(ContextMenu)
setText
public final void setText(String value)
- Property description:
- The text to display in the
MenuItem.
getText
public final String getText()
- Property description:
- The text to display in the
MenuItem.
textProperty
public final StringProperty textProperty()
MenuItem.- See Also:
-
getText(),setText(String)
setGraphic
public final void setGraphic(Node value)
- Property description:
- An optional graphic for the
MenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.
getGraphic
public final Node getGraphic()
- Property description:
- An optional graphic for the
MenuItem. This will normally be anImageViewnode, but there is no requirement for this to be the case.
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.- See Also:
-
getGraphic(),setGraphic(Node)
setOnAction
public final void setOnAction(EventHandler<ActionEvent> value)
- 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.
getOnAction
public final EventHandler<ActionEvent> getOnAction()
- 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.
onActionProperty
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty()
fire() method.- See Also:
-
getOnAction(),setOnAction(EventHandler)
setOnMenuValidation
public final void setOnMenuValidation(EventHandler<Event> value)
- 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
getOnMenuValidation
public final EventHandler<Event> getOnMenuValidation()
- 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
onMenuValidationProperty
public final ObjectProperty<EventHandler<Event>> onMenuValidationProperty()
- Since:
- JavaFX 2.2
- See Also:
-
getOnMenuValidation(),setOnMenuValidation(EventHandler)
setDisable
public final void setDisable(boolean value)
- Property description:
- Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.
isDisable
public final boolean isDisable()
- Property description:
- Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.
disableProperty
public final BooleanProperty disableProperty()
- See Also:
-
isDisable(),setDisable(boolean)
setVisible
public final void setVisible(boolean value)
- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
isVisible
public final boolean isVisible()
- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
visibleProperty
public final BooleanProperty visibleProperty()
- See Also:
-
isVisible(),setVisible(boolean)
setAccelerator
public final void setAccelerator(KeyCombination value)
- Property description:
- The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.
getAccelerator
public final KeyCombination getAccelerator()
- Property description:
- The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.
acceleratorProperty
public final ObjectProperty<KeyCombination> acceleratorProperty()
- See Also:
-
getAccelerator(),setAccelerator(KeyCombination)
setMnemonicParsing
public final void setMnemonicParsing(boolean value)
- 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.
isMnemonicParsing
public final boolean isMnemonicParsing()
- 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.
mnemonicParsingProperty
public final BooleanProperty mnemonicParsingProperty()
The default value for MenuItem is true.
- See Also:
-
isMnemonicParsing(),setMnemonicParsing(boolean)
fire
public void fire()
addEventHandler
public <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
Event of 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
removeEventHandler
public <E extends Event> void removeEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
- 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
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
© 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.