Class MenuItem
- All Implemented Interfaces:
Styleable
,EventTarget
- Direct Known Subclasses:
CheckMenuItem
,CustomMenuItem
,Menu
,RadioMenuItem
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
TypePropertyDescriptionfinal ObjectProperty
<KeyCombination> The accelerator property enables accessing the associated action in one keystroke.final BooleanProperty
Sets the individual disabled state of this MenuItem.final ObjectProperty
<Node> An optional graphic for theMenuItem
.final StringProperty
The id of this MenuItem.final BooleanProperty
MnemonicParsing property to enable/disable text parsing.final ObjectProperty
<EventHandler<ActionEvent>> The action, which is invoked whenever the MenuItem is fired.final ObjectProperty
<EventHandler<Event>> The event handler that is associated with invocation of an accelerator for a MenuItem.final ReadOnlyObjectProperty
<Menu> This is theMenu
in which thisMenuItem
exists.This is theContextMenu
in which thisMenuItem
exists.final StringProperty
A string representation of the CSS style associated with this specific MenuItem.final StringProperty
The text to display in theMenuItem
.final BooleanProperty
Specifies whether this MenuItem should be rendered as part of the scene graph. -
Field Summary
Modifier and TypeFieldDescriptionCalled when a accelerator for the Menuitem is invoked -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal ObjectProperty
<KeyCombination> The accelerator property enables accessing the associated action in one keystroke.Construct an event dispatch chain for this target.final BooleanProperty
Sets the individual disabled state of this MenuItem.void
fire()
Fires a new ActionEvent.final KeyCombination
Gets the value of theaccelerator
property.List
<CssMetaData<? extends Styleable, ?>> The CssMetaData of this Styleable.final Node
Gets the value of thegraphic
property.final String
getId()
Gets the value of theid
property.final EventHandler
<ActionEvent> Gets the value of theonAction
property.final EventHandler
<Event> Gets the value of theonMenuValidation
property.final Menu
Gets the value of theparentMenu
property.final ContextMenu
Gets the value of theparentPopup
property.Returns an observable map of properties on this menu item for use primarily by application developers.final ObservableSet
<PseudoClass> Return the pseudo-class state of this Styleable.final String
getStyle()
Gets the value of thestyle
property.Return the parent of this Styleable, or null if there is no parent.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 thetext
property.The type of thisStyleable
that is to be used in selector matching.final ObjectProperty
<Node> An optional graphic for theMenuItem
.final StringProperty
The id of this MenuItem.final boolean
Gets the value of thedisable
property.final boolean
Gets the value of themnemonicParsing
property.final boolean
Gets the value of thevisible
property.final BooleanProperty
MnemonicParsing property to enable/disable text parsing.final ObjectProperty
<EventHandler<ActionEvent>> The action, which is invoked whenever the MenuItem is fired.final ObjectProperty
<EventHandler<Event>> The event handler that is associated with invocation of an accelerator for a MenuItem.final ReadOnlyObjectProperty
<Menu> This is theMenu
in which thisMenuItem
exists.This is theContextMenu
in which thisMenuItem
exists.final void
setAccelerator
(KeyCombination value) Sets the value of theaccelerator
property.final void
setDisable
(boolean value) Sets the value of thedisable
property.final void
setGraphic
(Node value) Sets the value of thegraphic
property.final void
Sets the value of theid
property.final void
setMnemonicParsing
(boolean value) Sets the value of themnemonicParsing
property.final void
setOnAction
(EventHandler<ActionEvent> value) Sets the value of theonAction
property.final void
setOnMenuValidation
(EventHandler<Event> value) Sets the value of theonMenuValidation
property.protected final void
setParentMenu
(Menu value) Sets the value of theparentMenu
property.protected final void
setParentPopup
(ContextMenu value) Sets the value of theparentPopup
property.final void
Sets the value of thestyle
property.final void
Sets the value of thetext
property.void
setUserData
(Object value) Stores a user object that contains data at their discretion.final void
setVisible
(boolean value) Sets the value of thevisible
property.final StringProperty
A string representation of the CSS style associated with this specific MenuItem.final StringProperty
The text to display in theMenuItem
.final BooleanProperty
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
The id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.- See Also:
-
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.- See Also:
-
parentMenu
This is theMenu
in which thisMenuItem
exists. 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 useMenu
internally.
- See Also:
- Not yet associated with its
-
parentPopup
This is theContextMenu
in which thisMenuItem
exists.- See Also:
-
text
-
graphic
An optional graphic for theMenuItem
. This will normally be anImageView
node, but there is no requirement for this to be the case.- See Also:
-
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 thefire()
method.- See Also:
-
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.- Since:
- JavaFX 2.2
- See Also:
-
disable
Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.- See Also:
-
visible
Specifies whether this MenuItem should be rendered as part of the scene graph.- See Also:
-
accelerator
The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.- See Also:
-
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.
- See Also:
-
-
Field Details
-
MENU_VALIDATION_EVENT
-
-
Constructor Details
-
MenuItem
public MenuItem()Constructs a MenuItem with no display text. -
MenuItem
Constructs a MenuItem and sets the display text with the specified text- Parameters:
text
- the display text- See Also:
-
MenuItem
-
-
Method Details
-
setId
Sets the value of theid
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 theid
property- See Also:
-
getId
-
idProperty
The id of this MenuItem. This simple string identifier is useful for finding a specific MenuItem within the scene graph.- Returns:
- the
id
property - See Also:
-
setStyle
Sets the value of thestyle
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 thestyle
property- See Also:
-
getStyle
Gets the value of thestyle
property.- Specified by:
getStyle
in 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
style
property - See Also:
-
styleProperty
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
style
property - See Also:
-
setParentMenu
Sets the value of theparentMenu
property.- Property description:
- This is the
Menu
in which thisMenuItem
exists. 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 useMenu
internally.
- Not yet associated with its
- Parameters:
value
- the value for theparentMenu
property- See Also:
-
getParentMenu
Gets the value of theparentMenu
property.- Property description:
- This is the
Menu
in which thisMenuItem
exists. 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 useMenu
internally.
- Not yet associated with its
- Returns:
- the value of the
parentMenu
property - See Also:
-
parentMenuProperty
This is theMenu
in which thisMenuItem
exists. 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 useMenu
internally.
- Returns:
- the
parentMenu
property - See Also:
- Not yet associated with its
-
setParentPopup
Sets the value of theparentPopup
property.- Property description:
- This is the
ContextMenu
in which thisMenuItem
exists. - Parameters:
value
- the value for theparentPopup
property- See Also:
-
getParentPopup
Gets the value of theparentPopup
property.- Property description:
- This is the
ContextMenu
in which thisMenuItem
exists. - Returns:
- the value of the
parentPopup
property - See Also:
-
parentPopupProperty
This is theContextMenu
in which thisMenuItem
exists.- Returns:
- the
parentPopup
property - See Also:
-
setText
Sets the value of thetext
property.- Property description:
- The text to display in the
MenuItem
. - Parameters:
value
- the value for thetext
property- See Also:
-
getText
Gets the value of thetext
property.- Property description:
- The text to display in the
MenuItem
. - Returns:
- the value of the
text
property - See Also:
-
textProperty
The text to display in theMenuItem
.- Returns:
- the
text
property - See Also:
-
setGraphic
-
getGraphic
-
graphicProperty
An optional graphic for theMenuItem
. This will normally be anImageView
node, but there is no requirement for this to be the case.- Returns:
- the
graphic
property - See Also:
-
setOnAction
Sets the value of theonAction
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 programmatically
invokes the
fire()
method. - Parameters:
value
- the value for theonAction
property- See Also:
-
getOnAction
Gets the value of theonAction
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 programmatically
invokes the
fire()
method. - Returns:
- the value of the
onAction
property - See Also:
-
onActionProperty
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 thefire()
method.- Returns:
- the
onAction
property - See Also:
-
setOnMenuValidation
Sets the value of theonMenuValidation
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 theonMenuValidation
property- Since:
- JavaFX 2.2
- See Also:
-
getOnMenuValidation
Gets the value of theonMenuValidation
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
onMenuValidation
property - Since:
- JavaFX 2.2
- See Also:
-
onMenuValidationProperty
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
onMenuValidation
property - Since:
- JavaFX 2.2
- See Also:
-
setDisable
public final void setDisable(boolean value) Sets the value of thedisable
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 thedisable
property- See Also:
-
isDisable
public final boolean isDisable()Gets the value of thedisable
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
disable
property - See Also:
-
disableProperty
Sets the individual disabled state of this MenuItem. Setting disable to true will cause this MenuItem to become disabled.- Returns:
- the
disable
property - See Also:
-
setVisible
public final void setVisible(boolean value) Sets the value of thevisible
property.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
- Parameters:
value
- the value for thevisible
property- See Also:
-
isVisible
public final boolean isVisible()Gets the value of thevisible
property.- Property description:
- Specifies whether this MenuItem should be rendered as part of the scene graph.
- Returns:
- the value of the
visible
property - See Also:
-
visibleProperty
Specifies whether this MenuItem should be rendered as part of the scene graph.- Returns:
- the
visible
property - See Also:
-
setAccelerator
Sets the value of theaccelerator
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 theaccelerator
property- See Also:
-
getAccelerator
Gets the value of theaccelerator
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
accelerator
property - See Also:
-
acceleratorProperty
The accelerator property enables accessing the associated action in one keystroke. It is a convenience offered to perform quickly a given action.- Returns:
- the
accelerator
property - See Also:
-
setMnemonicParsing
public final void setMnemonicParsing(boolean value) Sets the value of themnemonicParsing
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 themnemonicParsing
property- See Also:
-
isMnemonicParsing
public final boolean isMnemonicParsing()Gets the value of themnemonicParsing
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
mnemonicParsing
property - See Also:
-
mnemonicParsingProperty
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
mnemonicParsing
property - See Also:
-
getStyleClass
Description copied from interface:Styleable
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.- Specified by:
getStyleClass
in 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()Fires a new ActionEvent. -
buildEventDispatchChain
Description copied from interface:EventTarget
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 thisEventTarget
. This event target is not automatically added to the chain, so if it wants to process events, it needs to add anEventDispatcher
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:
buildEventDispatchChain
in interfaceEventTarget
- Parameters:
tail
- the initial chain to build from- Returns:
- the resulting event dispatch chain for this target
-
getUserData
- Returns:
- the user object that was stored (including
null
), ornull
if no object has been set
-
setUserData
Stores a user object that contains data at their discretion. It can later be retrieved by callinggetUserData()
.- Parameters:
value
- the user object to be stored
-
getProperties
Returns an observable map of properties on this menu item for use primarily by application developers.- Returns:
- an observable map of properties on this menu item for use primarily by application developers
-
getTypeSelector
The type of thisStyleable
that is to be used in selector matching. This is analogous to an "element" in HTML. (CSS Type Selector).- Specified by:
getTypeSelector
in interfaceStyleable
- Returns:
- "MenuItem"
- Since:
- JavaFX 8.0
-
getStyleableParent
Return the parent of this Styleable, or null if there is no parent.- Specified by:
getStyleableParent
in interfaceStyleable
- Returns:
getParentMenu()
, orgetParentPopup()
ifparentMenu
is null- Since:
- JavaFX 8.0
-
getPseudoClassStates
Return the pseudo-class state of this Styleable. CSS assumes this set is read-only.- Specified by:
getPseudoClassStates
in interfaceStyleable
- Returns:
- the pseudo-class state
- Since:
- JavaFX 8.0
-
getCssMetaData
Description copied from interface:Styleable
The CssMetaData of this Styleable. This may be returned as an unmodifiable list.- Specified by:
getCssMetaData
in interfaceStyleable
- Returns:
- the CssMetaData
-