@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 |
ObjectProperty<EventHandler<Event>> |
onHiding |
ObjectProperty<EventHandler<Event>> |
onShowing |
ObjectProperty<EventHandler<Event>> |
onShown |
ReadOnlyBooleanProperty |
showing
*
Properties *
*
|
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 contentMenu for this menu is hidden.
|
static EventType<Event> |
ON_HIDING
Called when the contentMenu for this menu will be hidden.
|
static EventType<Event> |
ON_SHOWING
Called when the contentMenu for this menu will be shown.
|
static EventType<Event> |
ON_SHOWN
Called when the contentMenu for this menu shows.
|
MENU_VALIDATION_EVENT
Constructor and Description |
---|
Menu()
Constructs a Menu with an empty string for its display text.
|
Menu(java.lang.String text)
Constructs a Menu and sets the display text with the specified text.
|
Menu(java.lang.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. |
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() |
ObjectProperty<EventHandler<Event>> |
onHidingProperty() |
ObjectProperty<EventHandler<Event>> |
onShowingProperty() |
ObjectProperty<EventHandler<Event>> |
onShownProperty() |
<E extends Event> |
removeEventHandler(EventType<E> eventType,
EventHandler<E> eventHandler)
Unregisters a previously registered event handler from this MenuItem.
|
void |
setOnHidden(EventHandler<Event> value)
Called just after the
ContextMenu has been hidden. |
void |
setOnHiding(EventHandler<Event> value)
Called just prior to the
ContextMenu being hidden. |
void |
setOnShowing(EventHandler<Event> value)
Called just prior to the
ContextMenu being shown, even if the menu has
no items to show. |
void |
setOnShown(EventHandler<Event> value)
Called just after the
ContextMenu is shown. |
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()
*
Properties *
*
|
acceleratorProperty, disableProperty, fire, getAccelerator, getGraphic, getId, getOnAction, getOnMenuValidation, getParentMenu, getParentPopup, getProperties, getStyle, getStyleClass, getText, 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, visibleProperty
public final ReadOnlyBooleanProperty showingProperty
isShowing()
public final ObjectProperty<EventHandler<Event>> onShowingProperty
getOnShowing()
,
setOnShowing(EventHandler)
public final ObjectProperty<EventHandler<Event>> onShownProperty
getOnShown()
,
setOnShown(EventHandler)
public final ObjectProperty<EventHandler<Event>> onHidingProperty
getOnHiding()
,
setOnHiding(EventHandler)
public final ObjectProperty<EventHandler<Event>> onHiddenProperty
getOnHidden()
,
setOnHidden(EventHandler)
public static final EventType<Event> ON_SHOWING
Called when the contentMenu 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 contentMenu 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 contentMenu for this menu will be hidden. However if the contextMenu is empty then this will not be called.
public Menu()
public Menu(java.lang.String text)
public final boolean isShowing()
public final ReadOnlyBooleanProperty showingProperty()
isShowing()
public final ObjectProperty<EventHandler<Event>> onShowingProperty()
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()
public final ObjectProperty<EventHandler<Event>> onShownProperty()
getOnShown()
,
setOnShown(EventHandler)
public final void setOnShown(EventHandler<Event> value)
ContextMenu
is shown.public final EventHandler<Event> getOnShown()
public final ObjectProperty<EventHandler<Event>> onHidingProperty()
getOnHiding()
,
setOnHiding(EventHandler)
public final void setOnHiding(EventHandler<Event> value)
ContextMenu
being hidden.public final EventHandler<Event> getOnHiding()
public final ObjectProperty<EventHandler<Event>> onHiddenProperty()
getOnHidden()
,
setOnHidden(EventHandler)
public final void setOnHidden(EventHandler<Event> value)
ContextMenu
has been hidden.public final EventHandler<Event> getOnHidden()
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)
MenuItem
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)
MenuItem
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 fromCopyright (c) 2008, 2014, Oracle and/or its affiliates. All rights reserved.