@IDProperty(value="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(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
System.out.println("Opening Database Connection...");
}
});
menuItem.setGraphic(new ImageView(new Image("flower.png")));
Refer to the Menu
page to learn how to insert MenuItem into a menu
instance. Briefly however, you can insert the MenuItem from the previous
example into a Menu as such:
final Menu menu = new Menu("File");
menu.getItems().add(menuItem);
Menu
Type | Property and 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.
|
Modifier and Type | Field and Description |
---|---|
static EventType<Event> |
MENU_VALIDATION_EVENT
Called when a accelerator for the Menuitem is invoked
|
Constructor and 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. |
Modifier and Type | Method and 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.
|
EventDispatchChain |
buildEventDispatchChain(EventDispatchChain tail)
Construct an event dispatch chain for this target.
|
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.
|
List<CssMetaData<? extends Styleable,?>> |
getCssMetaData()
The CssMetaData of this Styleable.
|
Node |
getGraphic()
Gets the value of the property graphic.
|
String |
getId()
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.
|
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.
|
String |
getStyle()
Gets the value of the property style.
|
Styleable |
getStyleableParent()
Return the parent of this Styleable, or null if there is no parent.
|
ObservableList<String> |
getStyleClass()
*
Public API *
*
|
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()
Returns a previously set Object property, or null if no such property
has been set using the
setUserData(java.lang.Object) method. |
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)
Convenience method for setting a single Object property that can be
retrieved at a later date.
|
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 . |
String |
toString() |
BooleanProperty |
visibleProperty()
Specifies whether this MenuItem should be rendered as part of the scene graph.
|
public final StringProperty idProperty
getId()
,
setId(String)
public final StringProperty styleProperty
getStyle()
,
setStyle(String)
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:
parentMenu
.
Menu
(i.e. it is a context menu, attached directly to a
MenuBar
, MenuButton
, or any of the other controls that use
Menu
internally.
getParentMenu()
,
setParentMenu(Menu)
public final ReadOnlyObjectProperty<ContextMenu> parentPopupProperty
ContextMenu
in which this MenuItem
exists.getParentPopup()
,
setParentPopup(ContextMenu)
public final StringProperty textProperty
MenuItem
.getText()
,
setText(String)
public final ObjectProperty<Node> graphicProperty
MenuItem
. This will normally be
an ImageView
node, but there is no requirement for this to be
the case.getGraphic()
,
setGraphic(Node)
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty
fire()
method.getOnAction()
,
setOnAction(EventHandler)
public final ObjectProperty<EventHandler<Event>> onMenuValidationProperty
getOnMenuValidation()
,
setOnMenuValidation(EventHandler)
public final BooleanProperty disableProperty
isDisable()
,
setDisable(boolean)
public final BooleanProperty visibleProperty
isVisible()
,
setVisible(boolean)
public final ObjectProperty<KeyCombination> acceleratorProperty
getAccelerator()
,
setAccelerator(KeyCombination)
public final BooleanProperty mnemonicParsingProperty
The default value for MenuItem is true.
isMnemonicParsing()
,
setMnemonicParsing(boolean)
public MenuItem()
public MenuItem(String text)
setText(java.lang.String)
public final void setId(String value)
public final String getId()
public final StringProperty idProperty()
getId()
,
setId(String)
public final void setStyle(String value)
public final String getStyle()
getStyle
in interface Styleable
public final StringProperty styleProperty()
getStyle()
,
setStyle(String)
protected final void setParentMenu(Menu value)
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:
parentMenu
.
Menu
(i.e. it is a context menu, attached directly to a
MenuBar
, MenuButton
, or any of the other controls that use
Menu
internally.
public final Menu getParentMenu()
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:
parentMenu
.
Menu
(i.e. it is a context menu, attached directly to a
MenuBar
, MenuButton
, or any of the other controls that use
Menu
internally.
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:
parentMenu
.
Menu
(i.e. it is a context menu, attached directly to a
MenuBar
, MenuButton
, or any of the other controls that use
Menu
internally.
getParentMenu()
,
setParentMenu(Menu)
protected final void setParentPopup(ContextMenu value)
ContextMenu
in which this MenuItem
exists.public final ContextMenu getParentPopup()
ContextMenu
in which this MenuItem
exists.public final ReadOnlyObjectProperty<ContextMenu> parentPopupProperty()
ContextMenu
in which this MenuItem
exists.getParentPopup()
,
setParentPopup(ContextMenu)
public final void setText(String value)
MenuItem
.public final String getText()
MenuItem
.public final StringProperty textProperty()
MenuItem
.getText()
,
setText(String)
public final void setGraphic(Node value)
MenuItem
. This will normally be
an ImageView
node, but there is no requirement for this to be
the case.public final Node getGraphic()
MenuItem
. This will normally be
an ImageView
node, but there is no requirement for this to be
the case.public final ObjectProperty<Node> graphicProperty()
MenuItem
. This will normally be
an ImageView
node, but there is no requirement for this to be
the case.getGraphic()
,
setGraphic(Node)
public final void setOnAction(EventHandler<ActionEvent> value)
fire()
method.public final EventHandler<ActionEvent> getOnAction()
fire()
method.public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty()
fire()
method.getOnAction()
,
setOnAction(EventHandler)
public final void setOnMenuValidation(EventHandler<Event> value)
public final EventHandler<Event> getOnMenuValidation()
public final ObjectProperty<EventHandler<Event>> onMenuValidationProperty()
getOnMenuValidation()
,
setOnMenuValidation(EventHandler)
public final void setDisable(boolean value)
public final boolean isDisable()
public final BooleanProperty disableProperty()
isDisable()
,
setDisable(boolean)
public final void setVisible(boolean value)
public final boolean isVisible()
public final BooleanProperty visibleProperty()
isVisible()
,
setVisible(boolean)
public final void setAccelerator(KeyCombination value)
public final KeyCombination getAccelerator()
public final ObjectProperty<KeyCombination> acceleratorProperty()
getAccelerator()
,
setAccelerator(KeyCombination)
public final void setMnemonicParsing(boolean value)
The default value for MenuItem is true.
public final boolean isMnemonicParsing()
The default value for MenuItem is true.
public final BooleanProperty mnemonicParsingProperty()
The default value for MenuItem is true.
isMnemonicParsing()
,
setMnemonicParsing(boolean)
public ObservableList<String> getStyleClass()
getStyleClass
in interface Styleable
public void fire()
public <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
Event
of the specified type during the bubbling
phase of event delivery.E
- the specific event class of the handlereventType
- the type of the events to receive by the handlereventHandler
- the handler to registerNullPointerException
- if the event type or handler is nullpublic <E extends Event> void removeEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
E
- the specific event class of the handlereventType
- the event type from which to unregistereventHandler
- the handler to unregisterNullPointerException
- if the event type or handler is nullpublic EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)
EventTarget
. 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.
buildEventDispatchChain
in interface EventTarget
tail
- the initial chain to build frompublic Object getUserData()
setUserData(java.lang.Object)
method.public void setUserData(Object value)
Node.getUserData()
.value
- The value to be stored - this can later be retrieved by calling
Node.getUserData()
.public ObservableMap<Object,Object> getProperties()
public String getTypeSelector()
Styleable
that is to be used in selector matching.
This is analogous to an "element" in HTML.
(CSS Type Selector).getTypeSelector
in interface Styleable
public Styleable getStyleableParent()
getStyleableParent
in interface Styleable
getParentMenu()
, or getParentPopup()
if parentMenu
is nullpublic final ObservableSet<PseudoClass> getPseudoClassStates()
getPseudoClassStates
in interface Styleable
public List<CssMetaData<? extends Styleable,?>> getCssMetaData()
Styleable
getCssMetaData
in interface Styleable
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 2008, 2017, Oracle and/or its affiliates. All rights reserved. Use is subject to license terms. Also see the documentation redistribution policy.