public class MenuItem
extends java.lang.Object
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(<any>)
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 |
---|---|
<any> |
accelerator
The accelerator property enables accessing the associated action in one keystroke.
|
BooleanProperty |
disable
Sets the individual disabled state of this MenuItem.
|
<any> |
graphic
An optional graphic for the
MenuItem . |
StringProperty |
id
The id of this MenuItem.
|
BooleanProperty |
mnemonicParsing
MnemonicParsing property to enable/disable text parsing.
|
<any> |
onAction
The action, which is invoked whenever the MenuItem is fired.
|
<any> |
onMenuValidation
The event handler that is associated with invocation of an accelerator for a MenuItem.
|
<any> |
parentMenu
This is the
Menu in which this MenuItem exists. |
<any> |
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 |
---|---|
private <any> |
accelerator
The accelerator property enables accessing the associated action in one keystroke.
|
private static java.lang.String |
DEFAULT_STYLE_CLASS
*
Stylesheet Handling *
*
|
private BooleanProperty |
disable
Sets the individual disabled state of this MenuItem.
|
(package private) EventHandlerManager |
eventHandlerManager |
private <any> |
graphic
An optional graphic for the
MenuItem . |
private StringProperty |
id
The id of this MenuItem.
|
static <any> |
MENU_VALIDATION_EVENT
Called when a accelerator for the Menuitem is invoked
|
private BooleanProperty |
mnemonicParsing
MnemonicParsing property to enable/disable text parsing.
|
private <any> |
onAction
The action, which is invoked whenever the MenuItem is fired.
|
private <any> |
onMenuValidation
The event handler that is associated with invocation of an accelerator for a MenuItem.
|
private <any> |
parentMenu
This is the
Menu in which this MenuItem exists. |
private <any> |
parentPopup
This is the
ContextMenu in which this MenuItem exists. |
private <any> |
properties |
private StringProperty |
style
A string representation of the CSS style associated with this specific MenuItem.
|
private <any> |
styleClass
*
Instance Variables *
*
|
private StringProperty |
text
The text to display in the
MenuItem . |
private java.lang.Object |
userData |
private BooleanProperty |
visible
Specifies whether this MenuItem should be rendered as part of the scene graph.
|
Constructor and Description |
---|
MenuItem()
Constructs a MenuItem with no display text.
|
MenuItem(java.lang.String text)
Constructs a MenuItem and sets the display text with the specified text
|
MenuItem(java.lang.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 |
---|---|
<any> |
acceleratorProperty()
The accelerator property enables accessing the associated action in one keystroke.
|
<E extends Event> |
addEventHandler(<any> eventType,
<any> eventHandler)
Registers an event handler to this MenuItem.
|
EventDispatchChain |
buildEventDispatchChain(EventDispatchChain tail) |
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.
|
java.util.List<<any>> |
getCssMetaData() |
Node |
getGraphic()
Gets the value of the property graphic.
|
java.lang.String |
getId()
Gets the value of the property id.
|
<any> |
getOnAction()
Gets the value of the property onAction.
|
<any> |
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.
|
<any> |
getProperties()
Returns an observable map of properties on this menu item for use primarily
by application developers.
|
<any> |
getPseudoClassStates() |
java.lang.String |
getStyle()
Gets the value of the property style.
|
Node |
getStyleableNode() |
Styleable |
getStyleableParent() |
<any> |
getStyleClass()
*
Public API *
*
|
java.lang.String |
getText()
Gets the value of the property text.
|
java.lang.String |
getTypeSelector() |
java.lang.Object |
getUserData()
Returns a previously set Object property, or null if no such property
has been set using the
setUserData(java.lang.Object) method. |
<any> |
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.
|
<any> |
onActionProperty()
The action, which is invoked whenever the MenuItem is fired.
|
<any> |
onMenuValidationProperty()
The event handler that is associated with invocation of an accelerator for a MenuItem.
|
<any> |
parentMenuProperty()
This is the
Menu in which this MenuItem exists. |
private <any> |
parentMenuPropertyImpl() |
<any> |
parentPopupProperty()
This is the
ContextMenu in which this MenuItem exists. |
private <any> |
parentPopupPropertyImpl() |
<E extends Event> |
removeEventHandler(<any> eventType,
<any> 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(java.lang.String value)
Sets the value of the property id.
|
void |
setMnemonicParsing(boolean value)
Sets the value of the property mnemonicParsing.
|
void |
setOnAction(<any> value)
Sets the value of the property onAction.
|
void |
setOnMenuValidation(<any> 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(java.lang.String value)
Sets the value of the property style.
|
void |
setText(java.lang.String value)
Sets the value of the property text.
|
void |
setUserData(java.lang.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 . |
java.lang.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 <any> 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 <any> parentPopupProperty
ContextMenu
in which this MenuItem
exists.getParentPopup()
,
setParentPopup(ContextMenu)
public final StringProperty textProperty
MenuItem
.getText()
,
setText(String)
public final <any> graphicProperty
MenuItem
. This will normally be
an javafx.scene.image.ImageView
node, but there is no requirement for this to be
the case.getGraphic()
,
setGraphic(Node)
public final <any> onActionProperty
fire()
method.getOnAction()
,
#setOnAction()
public final <any> onMenuValidationProperty
getOnMenuValidation()
,
#setOnMenuValidation()
public final BooleanProperty disableProperty
isDisable()
,
setDisable(boolean)
public final BooleanProperty visibleProperty
isVisible()
,
setVisible(boolean)
public final <any> acceleratorProperty
getAccelerator()
,
setAccelerator(KeyCombination)
public final BooleanProperty mnemonicParsingProperty
The default value for MenuItem is true.
isMnemonicParsing()
,
setMnemonicParsing(boolean)
private final <any> styleClass
final EventHandlerManager eventHandlerManager
private java.lang.Object userData
private <any> properties
private StringProperty id
private StringProperty style
private <any> parentMenu
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.
private <any> parentPopup
ContextMenu
in which this MenuItem
exists.private StringProperty text
MenuItem
.private <any> graphic
MenuItem
. This will normally be
an javafx.scene.image.ImageView
node, but there is no requirement for this to be
the case.private <any> onAction
fire()
method.public static final <any> MENU_VALIDATION_EVENT
Called when a accelerator for the Menuitem is invoked
private <any> onMenuValidation
private BooleanProperty disable
private BooleanProperty visible
private <any> accelerator
private BooleanProperty mnemonicParsing
The default value for MenuItem is true.
private static final java.lang.String DEFAULT_STYLE_CLASS
public MenuItem()
public MenuItem(java.lang.String text)
text
- the display textsetText(java.lang.String)
public MenuItem(java.lang.String text, Node graphic)
Node
to the given node.text
- the display textgraphic
- the graphic nodesetText(java.lang.String)
,
setGraphic(Node)
public final void setId(java.lang.String value)
public final java.lang.String getId()
public final StringProperty idProperty()
getId()
,
setId(String)
public final void setStyle(java.lang.String value)
public final java.lang.String getStyle()
public final StringProperty styleProperty()
getStyle()
,
setStyle(String)
protected final void setParentMenu(Menu value)
public final Menu getParentMenu()
public final <any> 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)
private <any> parentMenuPropertyImpl()
protected final void setParentPopup(ContextMenu value)
public final ContextMenu getParentPopup()
public final <any> parentPopupProperty()
ContextMenu
in which this MenuItem
exists.getParentPopup()
,
setParentPopup(ContextMenu)
private <any> parentPopupPropertyImpl()
public final void setText(java.lang.String value)
public final java.lang.String getText()
public final StringProperty textProperty()
MenuItem
.getText()
,
setText(String)
public final void setGraphic(Node value)
public final Node getGraphic()
public final <any> graphicProperty()
MenuItem
. This will normally be
an javafx.scene.image.ImageView
node, but there is no requirement for this to be
the case.getGraphic()
,
setGraphic(Node)
public final void setOnAction(<any> value)
public final <any> getOnAction()
public final <any> onActionProperty()
fire()
method.getOnAction()
,
#setOnAction()
public final void setOnMenuValidation(<any> value)
public final <any> getOnMenuValidation()
public final <any> onMenuValidationProperty()
getOnMenuValidation()
,
#setOnMenuValidation()
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 <any> acceleratorProperty()
getAccelerator()
,
setAccelerator(KeyCombination)
public final void setMnemonicParsing(boolean value)
public final boolean isMnemonicParsing()
public final BooleanProperty mnemonicParsingProperty()
The default value for MenuItem is true.
isMnemonicParsing()
,
setMnemonicParsing(boolean)
public <any> getStyleClass()
public void fire()
public <E extends Event> void addEventHandler(<any> eventType, <any> 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 registerjava.lang.NullPointerException
- if the event type or handler is nullpublic <E extends Event> void removeEventHandler(<any> eventType, <any> eventHandler)
E
- the specific event class of the handlereventType
- the event type from which to unregistereventHandler
- the handler to unregisterjava.lang.NullPointerException
- if the event type or handler is nullpublic EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)
public java.lang.Object getUserData()
setUserData(java.lang.Object)
method.public void setUserData(java.lang.Object value)
Node#getUserData()
.value
- The value to be stored - this can later be retrieved by calling
Node#getUserData()
.public <any> getProperties()
public java.lang.String getTypeSelector()
public Styleable getStyleableParent()
getParentMenu()
, or getParentPopup()
if parentMenu
is nullpublic final <any> getPseudoClassStates()
public java.util.List<<any>> getCssMetaData()
public Node getStyleableNode()
public java.lang.String toString()
toString
in class java.lang.Object