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);
Menu
| Modifier and Type | Field and Description |
|---|---|
| private ObjectProperty | 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 | graphic
An optional graphic for the |
| private StringProperty | id
The id of this MenuItem. |
| public static final EventType | MENU_VALIDATION_EVENT
Called when a accelerator for the Menuitem is invoked |
| private BooleanProperty | mnemonicParsing
MnemonicParsing property to enable/disable text parsing. |
| private ObjectProperty | onAction
The action, which is invoked whenever the MenuItem is fired. |
| private ObjectProperty | onMenuValidation
The event handler that is associated with invocation of an accelerator for a MenuItem. |
| private ReadOnlyObjectWrapper | parentMenu
This is the |
| private ReadOnlyObjectWrapper | parentPopup
This is the |
| private ObservableMap | |
| private StringProperty | style
A string representation of the CSS style associated with this specific MenuItem. |
| private final ObservableList | |
| private StringProperty | text
The text to display in the |
| private Object | |
| private BooleanProperty | visible
Specifies whether this MenuItem should be rendered as part of the scene graph. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public final ObjectProperty | |
| public <E extends Event> void | addEventFilter(EventType<E>
the type of the events received by the filter eventType, EventHandler<? super E> the event filter eventFilter)Overrides default javafx. Registers an event filter for this target. |
| public <E extends Event> void | addEventHandler(EventType<E>
the type of the events received by the handler eventType, EventHandler<? super E> the event handler eventHandler)Overrides default javafx. Registers an event handler for this target. |
| public EventDispatchChain | buildEventDispatchChain(EventDispatchChain
the initial chain to build from tail)Implements javafx. Construct an event dispatch chain for this target. |
| public final BooleanProperty | |
| public void | |
| public final KeyCombination | |
| public List | |
| public final Node | |
| public final String | |
| public final EventHandler | |
| public final EventHandler | |
| public final Menu | |
| public final ContextMenu | |
| public ObservableMap | Returns: an observable map of properties on this menu item for use primarily by application developersReturns an observable map of properties on this menu item for use primarily by application developers. |
| public final ObservableSet | getPseudoClassStates()
Implements javafx. Return the pseudo-class state of this Styleable. |
| public final String | getStyle()
Implements javafx. A string representation of the CSS style associated with this
specific |
| public Node | getStyleableNode()
Overrides default javafx. Returns the Node that represents this Styleable object. |
| public Styleable | Returns: getParentMenu(), or getParentPopup()
if parentMenu is nullImplements javafx. Return the parent of this Styleable, or null if there is no parent. |
| public ObservableList | getStyleClass()
Implements javafx. 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"Implements javafx. The type of this |
| public Object | Returns: the user object that was stored (includingnull), or null if no object has been setReturns the |
| public final ObjectProperty | |
| public final StringProperty | |
| public final boolean | |
| public final boolean | |
| public final boolean | |
| public final BooleanProperty | |
| public final ObjectProperty | |
| public final ObjectProperty | |
| public final ReadOnlyObjectProperty | |
| private ReadOnlyObjectWrapper | |
| public final ReadOnlyObjectProperty | |
| private ReadOnlyObjectWrapper | |
| public <E extends Event> void | removeEventFilter(EventType<E>
the event type from which to unregister eventType, EventHandler<? super E> the event filter eventFilter)Overrides default javafx. Unregisters a previously registered event filter from this target. |
| public <E extends Event> void | removeEventHandler(EventType<E>
the event type from which to unregister eventType, EventHandler<? super E> the event handler eventHandler)Overrides default javafx. Unregisters a previously registered event handler from this target. |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| public final void | |
| protected final void | |
| protected final void | |
| public final void | |
| public final void | |
| public void | setUserData(Object
the user object to be stored value)Stores a user object that contains data at their discretion. |
| public final void | |
| public final StringProperty | |
| public final StringProperty | |
| public String | |
| public final BooleanProperty |
| accelerator | back 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_CLASS | back to summary |
|---|---|
| private static final String DEFAULT_STYLE_CLASS | |
| disable | back to summary |
|---|---|
| private BooleanProperty disable Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled. | |
| eventHandlerManager | back to summary |
|---|---|
| pack-priv final EventHandlerManager eventHandlerManager | |
| graphic | back to summary |
|---|---|
| private ObjectProperty<Node> graphic An optional graphic for the | |
| id | back 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. | |
| MENU_VALIDATION_EVENT | back to summary |
|---|---|
| public static final EventType<Event> MENU_VALIDATION_EVENT Called when a accelerator for the Menuitem is invoked
| |
| mnemonicParsing | back 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. | |
| onAction | back 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 | |
| onMenuValidation | back 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.
| |
| parentMenu | back to summary |
|---|---|
| private ReadOnlyObjectWrapper<Menu> parentMenu This is the
| |
| parentPopup | back to summary |
|---|---|
| private ReadOnlyObjectWrapper<ContextMenu> parentPopup This is the | |
| properties | back to summary |
|---|---|
| private ObservableMap<Object, Object> properties | |
| style | back 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. | |
| styleClass | back to summary |
|---|---|
| private final ObservableList<String> styleClass | |
| text | back to summary |
|---|---|
| private StringProperty text The text to display in the | |
| userData | back to summary |
|---|---|
| private Object userData | |
| visible | back to summary |
|---|---|
| private BooleanProperty visible Specifies whether this MenuItem should be rendered as part of the scene graph. | |
| MenuItem | back to summary |
|---|---|
| public MenuItem() Constructs a MenuItem with no display text. | |
| MenuItem | back to summary |
|---|---|
| public MenuItem(String text) Constructs a MenuItem and sets the display text with the specified text | |
| MenuItem | back to summary |
|---|---|
| public MenuItem(String text, Node graphic) Constructor s MenuItem and sets the display text with the specified text
and sets the graphic
| |
| acceleratorProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| addEventFilter | back to summary |
|---|---|
| public <E extends Event> void addEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Overrides default javafx. Doc from javafx. Registers an event filter for this target.
The filter is called when the target receives an
| |
| addEventHandler | back to summary |
|---|---|
| public <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Overrides default javafx. Doc from javafx. Registers an event handler for this target.
The handler is called when the target receives an
| |
| buildEventDispatchChain | back to summary |
|---|---|
| public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail) Implements javafx. Doc from javafx. 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 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.
| |
| disableProperty | back to summary |
|---|---|
| public final BooleanProperty disableProperty() | |
| fire | back to summary |
|---|---|
| public void fire() Fires a new ActionEvent. | |
| getAccelerator | back to summary |
|---|---|
| public final KeyCombination getAccelerator() | |
| getCssMetaData | back to summary |
|---|---|
| public List Implements javafx. Doc from javafx. The CssMetaData of this Styleable. This may be returned as an unmodifiable list.
| |
| getGraphic | back to summary |
|---|---|
| public final Node getGraphic() | |
| getId | back to summary |
|---|---|
| public final String getId() Implements javafx. Doc from javafx. The id of this
For example, if a Node is given the id of "myId", then the lookup method can
be used to find this node as follows: | |
| getOnAction | back to summary |
|---|---|
| public final EventHandler | |
| getOnMenuValidation | back to summary |
|---|---|
| public final EventHandler | |
| getParentMenu | back to summary |
|---|---|
| public final Menu getParentMenu() | |
| getParentPopup | back to summary |
|---|---|
| public final ContextMenu getParentPopup() | |
| getProperties | back to summary |
|---|---|
| public ObservableMap Returns an observable map of properties on this menu item for use primarily by application developers.
| |
| getPseudoClassStates | back to summary |
|---|---|
| public final ObservableSet Implements javafx. Doc from javafx. Return the pseudo-class state of this Styleable. CSS assumes this set is read-only.
| |
| getStyle | back to summary |
|---|---|
| public final String getStyle() Implements javafx. Doc from javafx. A string representation of the CSS style associated with this
specific | |
| getStyleableNode | back to summary |
|---|---|
| public Node getStyleableNode() Overrides default javafx. Doc from javafx. 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. | |
| getStyleableParent | back to summary |
|---|---|
| public Styleable getStyleableParent() Implements javafx. Doc from javafx. Return the parent of this Styleable, or null if there is no parent. | |
| getStyleClass | back to summary |
|---|---|
| public ObservableList Implements javafx. Doc from javafx. 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.
| |
| getText | back to summary |
|---|---|
| public final String getText() | |
| getTypeSelector | back to summary |
|---|---|
| public String getTypeSelector() Implements javafx. Doc from javafx. The type of this | |
| getUserData | back to summary |
|---|---|
| public Object getUserData() Returns the
| |
| graphicProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| idProperty | back to summary |
|---|---|
| public final StringProperty idProperty() | |
| isDisable | back to summary |
|---|---|
| public final boolean isDisable() | |
| isMnemonicParsing | back to summary |
|---|---|
| public final boolean isMnemonicParsing() | |
| isVisible | back to summary |
|---|---|
| public final boolean isVisible() | |
| mnemonicParsingProperty | back to summary |
|---|---|
| public final BooleanProperty mnemonicParsingProperty() | |
| onActionProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| onMenuValidationProperty | back to summary |
|---|---|
| public final ObjectProperty | |
| parentMenuProperty | back to summary |
|---|---|
| public final ReadOnlyObjectProperty | |
| parentMenuPropertyImpl | back to summary |
|---|---|
| private ReadOnlyObjectWrapper | |
| parentPopupProperty | back to summary |
|---|---|
| public final ReadOnlyObjectProperty | |
| parentPopupPropertyImpl | back to summary |
|---|---|
| private ReadOnlyObjectWrapper | |
| removeEventFilter | back to summary |
|---|---|
| public <E extends Event> void removeEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Overrides default javafx. Doc from javafx. Unregisters a previously registered event filter from this target.
Since it is possible to register a single
| |
| removeEventHandler | back to summary |
|---|---|
| public <E extends Event> void removeEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Overrides default javafx. Doc from javafx. Unregisters a previously registered event handler from this target.
Since it is possible to register a single
| |
| setAccelerator | back to summary |
|---|---|
| public final void setAccelerator(KeyCombination value) | |
| setDisable | back to summary |
|---|---|
| public final void setDisable(boolean value) | |
| setGraphic | back to summary |
|---|---|
| public final void setGraphic(Node value) | |
| setId | back to summary |
|---|---|
| public final void setId(String value) | |
| setMnemonicParsing | back to summary |
|---|---|
| public final void setMnemonicParsing(boolean value) | |
| setOnAction | back to summary |
|---|---|
| public final void setOnAction(EventHandler<ActionEvent> value) | |
| setOnMenuValidation | back to summary |
|---|---|
| public final void setOnMenuValidation(EventHandler<Event> value) | |
| setParentMenu | back to summary |
|---|---|
| protected final void setParentMenu(Menu value) | |
| setParentPopup | back to summary |
|---|---|
| protected final void setParentPopup(ContextMenu value) | |
| setStyle | back to summary |
|---|---|
| public final void setStyle(String value) | |
| setText | back to summary |
|---|---|
| public final void setText(String value) | |
| setUserData | back to summary |
|---|---|
| public void setUserData(Object value) Stores a user object that contains data at their discretion. It can later be retrieved by calling
| |
| setVisible | back to summary |
|---|---|
| public final void setVisible(boolean value) | |
| styleProperty | back to summary |
|---|---|
| public final StringProperty styleProperty() | |
| textProperty | back to summary |
|---|---|
| public final StringProperty textProperty() | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| visibleProperty | back to summary |
|---|---|
| public final BooleanProperty visibleProperty() | |