@DefaultProperty(value="items") public class Menu extends MenuItem
A popup menu of actionable items which is displayed to the user only upon request. When a menu is visible, in most use cases, the user can select one menu item before the menu goes back to its hidden state. This means the menu is a good place to put important functionality that does not necessarily need to be visible at all times to the user.
Menus are typically placed in a MenuBar
, or as a submenu of another Menu.
If the intention is to offer a context menu when the user right-clicks in a
certain area of their user interface, then this is the wrong control to use.
This is because when Menu is added to the scenegraph, it has a visual
representation that will result in it appearing on screen. Instead,
ContextMenu
should be used in this circumstance.
Creating a Menu and inserting it into a MenuBar is easy, as shown below:
final Menu menu1 = new Menu("File");
MenuBar menuBar = new MenuBar();
menuBar.getMenus().add(menu1);
A Menu is a subclass of MenuItem
which means that it can be inserted
into a Menu's items
ObservableList, resulting in a submenu being created:
MenuItem menu12 = new MenuItem("Open");
menu1.getItems().add(menu12);
The items ObservableList allows for any MenuItem
type to be inserted,
including its subclasses Menu
, MenuItem
, RadioMenuItem
, CheckMenuItem
,
CustomMenuItem
and SeparatorMenuItem
. In order to insert an arbitrary Node
to
a Menu, a CustomMenuItem can be used. One exception to this general rule is that
SeparatorMenuItem
could be used for inserting a separator.
Type | Property and Description |
---|---|
ObjectProperty<EventHandler<Event>> |
onHidden
Called just after the
ContextMenu has been hidden. |
ObjectProperty<EventHandler<Event>> |
onHiding
Called just prior to the
ContextMenu being hidden. |
ObjectProperty<EventHandler<Event>> |
onShowing
Called just prior to the
ContextMenu being shown, even if the menu has
no items to show. |
ObjectProperty<EventHandler<Event>> |
onShown
Called just after the
ContextMenu is shown. |
ReadOnlyBooleanProperty |
showing
Indicates whether the
ContextMenu is currently visible. |
accelerator, disable, graphic, id, mnemonicParsing, onAction, onMenuValidation, parentMenu, parentPopup, style, text, visible
Modifier and Type | Field and Description |
---|---|
static EventType<Event> |
ON_HIDDEN
Called when the contextMenu for this menu is hidden.
|
static EventType<Event> |
ON_HIDING
Called when the contextMenu for this menu will be hidden.
|
static EventType<Event> |
ON_SHOWING
Called when the contextMenu for this menu will be shown.
|
static EventType<Event> |
ON_SHOWN
Called when the contextMenu for this menu shows.
|
MENU_VALIDATION_EVENT
Constructor and Description |
---|
Menu()
Constructs a Menu with an empty string for its display text.
|
Menu(String text)
Constructs a Menu and sets the display text with the specified text.
|
Menu(String text,
Node graphic)
Constructs a Menu and sets the display text with the specified text
and sets the graphic
Node to the given node. |
Menu(String text,
Node graphic,
MenuItem... items)
|
Modifier and Type | Method and Description |
---|---|
<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.
|
ObservableList<MenuItem> |
getItems()
The items to show within this menu.
|
EventHandler<Event> |
getOnHidden()
Gets the value of the property onHidden.
|
EventHandler<Event> |
getOnHiding()
Gets the value of the property onHiding.
|
EventHandler<Event> |
getOnShowing()
Gets the value of the property onShowing.
|
EventHandler<Event> |
getOnShown()
Gets the value of the property onShown.
|
void |
hide()
Hides the
ContextMenu if it was previously showing, and any showing
submenus. |
boolean |
isShowing()
Gets the value of the property showing.
|
ObjectProperty<EventHandler<Event>> |
onHiddenProperty()
Called just after the
ContextMenu has been hidden. |
ObjectProperty<EventHandler<Event>> |
onHidingProperty()
Called just prior to the
ContextMenu being hidden. |
ObjectProperty<EventHandler<Event>> |
onShowingProperty()
Called just prior to the
ContextMenu being shown, even if the menu has
no items to show. |
ObjectProperty<EventHandler<Event>> |
onShownProperty()
Called just after the
ContextMenu is shown. |
<E extends Event> |
removeEventHandler(EventType<E> eventType,
EventHandler<E> eventHandler)
Unregisters a previously registered event handler from this MenuItem.
|
void |
setOnHidden(EventHandler<Event> value)
Sets the value of the property onHidden.
|
void |
setOnHiding(EventHandler<Event> value)
Sets the value of the property onHiding.
|
void |
setOnShowing(EventHandler<Event> value)
Sets the value of the property onShowing.
|
void |
setOnShown(EventHandler<Event> value)
Sets the value of the property onShown.
|
void |
show()
If the Menu is not disabled and the
ContextMenu is not already showing,
then this will cause the ContextMenu to be shown. |
ReadOnlyBooleanProperty |
showingProperty()
Indicates whether the
ContextMenu is currently visible. |
acceleratorProperty, disableProperty, fire, getAccelerator, getCssMetaData, getGraphic, getId, getOnAction, getOnMenuValidation, getParentMenu, getParentPopup, getProperties, getPseudoClassStates, getStyle, getStyleableParent, getStyleClass, getText, getTypeSelector, getUserData, graphicProperty, idProperty, isDisable, isMnemonicParsing, isVisible, mnemonicParsingProperty, onActionProperty, onMenuValidationProperty, parentMenuProperty, parentPopupProperty, setAccelerator, setDisable, setGraphic, setId, setMnemonicParsing, setOnAction, setOnMenuValidation, setParentMenu, setParentPopup, setStyle, setText, setUserData, setVisible, styleProperty, textProperty, toString, visibleProperty
public final ReadOnlyBooleanProperty showingProperty
ContextMenu
is currently visible.isShowing()
public final ObjectProperty<EventHandler<Event>> onShowingProperty
ContextMenu
being shown, even if the menu has
no items to show. Note however that this won't be called if the menu does
not have a valid anchor node.getOnShowing()
,
setOnShowing(EventHandler)
public final ObjectProperty<EventHandler<Event>> onShownProperty
ContextMenu
is shown.getOnShown()
,
setOnShown(EventHandler)
public final ObjectProperty<EventHandler<Event>> onHidingProperty
ContextMenu
being hidden.getOnHiding()
,
setOnHiding(EventHandler)
public final ObjectProperty<EventHandler<Event>> onHiddenProperty
ContextMenu
has been hidden.getOnHidden()
,
setOnHidden(EventHandler)
public static final EventType<Event> ON_SHOWING
Called when the contextMenu for this menu will be shown. However if the contextMenu is empty then this will not be called.
public static final EventType<Event> ON_SHOWN
Called when the contextMenu for this menu shows. However if the contextMenu is empty then this will not be called.
public static final EventType<Event> ON_HIDING
Called when the contextMenu for this menu will be hidden. However if the contextMenu is empty then this will not be called.
public Menu()
public Menu(String text)
text
- the text to display on the menu buttonpublic Menu(String text, Node graphic)
Node
to the given node.text
- the text to display on the menu buttongraphic
- the graphic to display on the menu buttonpublic Menu(String text, Node graphic, MenuItem... items)
Node
to the given node, and inserts the given items
into the items
list.text
- the text to display on the menu buttongraphic
- the graphic to display on the menu buttonitems
- The items to display in the popup menu.public final boolean isShowing()
ContextMenu
is currently visible.public final ReadOnlyBooleanProperty showingProperty()
ContextMenu
is currently visible.isShowing()
public final ObjectProperty<EventHandler<Event>> onShowingProperty()
ContextMenu
being shown, even if the menu has
no items to show. Note however that this won't be called if the menu does
not have a valid anchor node.getOnShowing()
,
setOnShowing(EventHandler)
public final void setOnShowing(EventHandler<Event> value)
ContextMenu
being shown, even if the menu has
no items to show. Note however that this won't be called if the menu does
not have a valid anchor node.public final EventHandler<Event> getOnShowing()
ContextMenu
being shown, even if the menu has
no items to show. Note however that this won't be called if the menu does
not have a valid anchor node.public final ObjectProperty<EventHandler<Event>> onShownProperty()
ContextMenu
is shown.getOnShown()
,
setOnShown(EventHandler)
public final void setOnShown(EventHandler<Event> value)
ContextMenu
is shown.public final EventHandler<Event> getOnShown()
ContextMenu
is shown.public final ObjectProperty<EventHandler<Event>> onHidingProperty()
ContextMenu
being hidden.getOnHiding()
,
setOnHiding(EventHandler)
public final void setOnHiding(EventHandler<Event> value)
ContextMenu
being hidden.public final EventHandler<Event> getOnHiding()
ContextMenu
being hidden.public final ObjectProperty<EventHandler<Event>> onHiddenProperty()
ContextMenu
has been hidden.getOnHidden()
,
setOnHidden(EventHandler)
public final void setOnHidden(EventHandler<Event> value)
ContextMenu
has been hidden.public final EventHandler<Event> getOnHidden()
ContextMenu
has been hidden.public final ObservableList<MenuItem> getItems()
public void show()
ContextMenu
is not already showing,
then this will cause the ContextMenu
to be shown.public void hide()
ContextMenu
if it was previously showing, and any showing
submenus. If this menu is not showing, then invoking this function
has no effect.public <E extends Event> void addEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
Event
of the specified type during the bubbling
phase of event delivery.addEventHandler
in class MenuItem
E
- the specific event class of the handlereventType
- the type of the events to receive by the handlereventHandler
- the handler to registerpublic <E extends Event> void removeEventHandler(EventType<E> eventType, EventHandler<E> eventHandler)
removeEventHandler
in class MenuItem
E
- the specific event class of the handlereventType
- the event type from which to unregistereventHandler
- the handler to unregisterpublic 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
buildEventDispatchChain
in class MenuItem
tail
- the initial chain to build fromSubmit 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.