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 |
---|---|
<any> |
onHidden
Called just after the
ContextMenu has been hidden. |
<any> |
onHiding
Called just prior to the
ContextMenu being hidden. |
<any> |
onShowing
Called just prior to the
ContextMenu being shown, even if the menu has
no items to show. |
<any> |
onShown
Called just after the
ContextMenu is shown. |
ReadOnlyBooleanProperty |
showing
Indicates whether the
ContextMenu is currently visible. |
acceleratorProperty, disableProperty, graphicProperty, idProperty, mnemonicParsingProperty, onActionProperty, onMenuValidationProperty, parentMenuProperty, parentPopupProperty, styleProperty, textProperty, visibleProperty
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
DEFAULT_STYLE_CLASS
*
Stylesheet Handling *
*
|
private <any> |
items
*
Instance variables *
*
|
static <any> |
ON_HIDDEN
Called when the contextMenu for this menu is hidden.
|
static <any> |
ON_HIDING
Called when the contextMenu for this menu will be hidden.
|
static <any> |
ON_SHOWING
Called when the contextMenu for this menu will be shown.
|
static <any> |
ON_SHOWN
Called when the contextMenu for this menu shows.
|
private <any> |
onHidden |
private <any> |
onHiding |
private <any> |
onShowing |
private <any> |
onShown |
private ReadOnlyBooleanWrapper |
showing
Indicates whether the
ContextMenu is currently visible. |
private static java.lang.String |
STYLE_CLASS_SHOWING |
eventHandlerManager, 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. |
Menu(java.lang.String text,
Node graphic,
MenuItem... items)
Constructs a Menu and sets the display text with the specified text,
the graphic
Node to the given node, and inserts the given items
into the items list. |
Modifier and Type | Method and Description |
---|---|
<E extends Event> |
addEventHandler(<any> eventType,
<any> eventHandler)
Registers an event handler to this MenuItem.
|
EventDispatchChain |
buildEventDispatchChain(EventDispatchChain tail) |
<any> |
getItems()
The items to show within this menu.
|
<any> |
getOnHidden()
Gets the value of the property onHidden.
|
<any> |
getOnHiding()
Gets the value of the property onHiding.
|
<any> |
getOnShowing()
Gets the value of the property onShowing.
|
<any> |
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.
|
<any> |
onHiddenProperty()
Called just after the
ContextMenu has been hidden. |
<any> |
onHidingProperty()
Called just prior to the
ContextMenu being hidden. |
<any> |
onShowingProperty()
Called just prior to the
ContextMenu being shown, even if the menu has
no items to show. |
<any> |
onShownProperty()
Called just after the
ContextMenu is shown. |
<E extends Event> |
removeEventHandler(<any> eventType,
<any> eventHandler)
Unregisters a previously registered event handler from this MenuItem.
|
void |
setOnHidden(<any> value)
Sets the value of the property onHidden.
|
void |
setOnHiding(<any> value)
Sets the value of the property onHiding.
|
void |
setOnShowing(<any> value)
Sets the value of the property onShowing.
|
void |
setOnShown(<any> value)
Sets the value of the property onShown.
|
private void |
setShowing(boolean value) |
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. |
private ReadOnlyBooleanWrapper |
showingPropertyImpl() |
acceleratorProperty, disableProperty, fire, getAccelerator, getCssMetaData, getGraphic, getId, getOnAction, getOnMenuValidation, getParentMenu, getParentPopup, getProperties, getPseudoClassStates, getStyle, getStyleableNode, 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 <any> 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()
public final <any> onShownProperty
ContextMenu
is shown.getOnShown()
,
#setOnShown()
public final <any> onHidingProperty
ContextMenu
being hidden.getOnHiding()
,
#setOnHiding()
public final <any> onHiddenProperty
ContextMenu
has been hidden.getOnHidden()
,
#setOnHidden()
public static final <any> 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 <any> 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 <any> 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 static final <any> ON_HIDDEN
Called when the contextMenu for this menu is hidden. However if the contextMenu is empty then this will not be called.
private ReadOnlyBooleanWrapper showing
ContextMenu
is currently visible.private <any> onShowing
private <any> onShown
private <any> onHiding
private <any> onHidden
private final <any> items
private static final java.lang.String DEFAULT_STYLE_CLASS
private static final java.lang.String STYLE_CLASS_SHOWING
public Menu()
public Menu(java.lang.String text)
text
- the text to display on the menu buttonpublic Menu(java.lang.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(java.lang.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.private void setShowing(boolean value)
public final boolean isShowing()
public final ReadOnlyBooleanProperty showingProperty()
ContextMenu
is currently visible.isShowing()
private ReadOnlyBooleanWrapper showingPropertyImpl()
public final <any> 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()
public final void setOnShowing(<any> value)
public final <any> getOnShowing()
public final <any> onShownProperty()
ContextMenu
is shown.getOnShown()
,
#setOnShown()
public final void setOnShown(<any> value)
public final <any> getOnShown()
public final <any> onHidingProperty()
ContextMenu
being hidden.getOnHiding()
,
#setOnHiding()
public final void setOnHiding(<any> value)
public final <any> getOnHiding()
public final <any> onHiddenProperty()
ContextMenu
has been hidden.getOnHidden()
,
#setOnHidden()
public final void setOnHidden(<any> value)
public final <any> getOnHidden()
public final <any> 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(<any> eventType, <any> 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(<any> eventType, <any> 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)
buildEventDispatchChain
in class MenuItem