Class Menu
- All Implemented Interfaces:
Styleable, EventTarget
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:
Menu menu1 = new Menu("File");
MenuBar menuBar = new MenuBar(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.
- Since:
- JavaFX 2.0
- See Also:
-
Property Summary
PropertiesTypePropertyDescriptionfinal ObjectProperty<EventHandler<Event>> Called just after theContextMenuhas been hidden.final ObjectProperty<EventHandler<Event>> Called just prior to theContextMenubeing hidden.final ObjectProperty<EventHandler<Event>> Called just prior to theContextMenubeing shown, even if the menu has no items to show.final ObjectProperty<EventHandler<Event>> Called just after theContextMenuis shown.final ReadOnlyBooleanPropertyIndicates whether theContextMenuis currently visible.Properties declared in class MenuItem
accelerator, disable, graphic, id, mnemonicParsing, onAction, onMenuValidation, parentMenu, parentPopup, style, text, visibleTypePropertyDescriptionfinal ObjectProperty<KeyCombination> The accelerator property enables accessing the associated action in one keystroke.final BooleanPropertySets the individual disabled state of this MenuItem.final ObjectProperty<Node> An optional graphic for theMenuItem.final StringPropertyThe id of this MenuItem.final BooleanPropertyDetermines whether the mnemonic character in the menu item text will be parsed.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 theMenuin which thisMenuItemexists.This is theContextMenuin which thisMenuItemexists.final StringPropertyA string representation of the CSS style associated with this specific MenuItem.final StringPropertyThe text to display in theMenuItem.final BooleanPropertySpecifies whether this MenuItem should be rendered as part of the scene graph. -
Field Summary
FieldsModifier and TypeFieldDescriptionCalled when the contextMenu for this menu is hidden.Called when the contextMenu for this menu will be hidden.Called when the contextMenu for this menu will be shown.Called when the contextMenu for this menu shows.Fields declared in class MenuItem
MENU_VALIDATION_EVENTModifier and TypeFieldDescriptionCalled when a accelerator for the Menuitem is invoked -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal ObservableList<MenuItem> getItems()The items to show within this menu.final EventHandler<Event> Gets the value of theonHiddenproperty.final EventHandler<Event> Gets the value of theonHidingproperty.final EventHandler<Event> Gets the value of theonShowingproperty.final EventHandler<Event> Gets the value of theonShownproperty.voidhide()Hides theContextMenuif it was previously showing, and any showing submenus.final booleanGets the value of theshowingproperty.final ObjectProperty<EventHandler<Event>> Called just after theContextMenuhas been hidden.final ObjectProperty<EventHandler<Event>> Called just prior to theContextMenubeing hidden.final ObjectProperty<EventHandler<Event>> Called just prior to theContextMenubeing shown, even if the menu has no items to show.final ObjectProperty<EventHandler<Event>> Called just after theContextMenuis shown.final voidsetOnHidden(EventHandler<Event> value) Sets the value of theonHiddenproperty.final voidsetOnHiding(EventHandler<Event> value) Sets the value of theonHidingproperty.final voidsetOnShowing(EventHandler<Event> value) Sets the value of theonShowingproperty.final voidsetOnShown(EventHandler<Event> value) Sets the value of theonShownproperty.voidshow()If the Menu is not disabled and theContextMenuis not already showing, then this will cause theContextMenuto be shown.final ReadOnlyBooleanPropertyIndicates whether theContextMenuis currently visible.Methods declared in class MenuItem
acceleratorProperty, buildEventDispatchChain, 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, visiblePropertyModifier and TypeMethodDescriptionfinal ObjectProperty<KeyCombination> The accelerator property enables accessing the associated action in one keystroke.Construct an event dispatch chain for this target.final BooleanPropertySets the individual disabled state of this MenuItem.voidfire()Fires a new ActionEvent.final KeyCombinationGets the value of theacceleratorproperty.List<CssMetaData<? extends Styleable, ?>> The CssMetaData of this Styleable.final NodeGets the value of thegraphicproperty.final StringgetId()Gets the value of theidproperty.final EventHandler<ActionEvent> Gets the value of theonActionproperty.final EventHandler<Event> Gets the value of theonMenuValidationproperty.final MenuGets the value of theparentMenuproperty.final ContextMenuGets the value of theparentPopupproperty.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 StringgetStyle()Gets the value of thestyleproperty.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 StringgetText()Gets the value of thetextproperty.The type of thisStyleablethat is to be used in selector matching.final ObjectProperty<Node> An optional graphic for theMenuItem.final StringPropertyThe id of this MenuItem.final booleanGets the value of thedisableproperty.final booleanGets the value of themnemonicParsingproperty.final booleanGets the value of thevisibleproperty.final BooleanPropertyDetermines whether the mnemonic character in the menu item text will be parsed.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 theMenuin which thisMenuItemexists.This is theContextMenuin which thisMenuItemexists.final voidsetAccelerator(KeyCombination value) Sets the value of theacceleratorproperty.final voidsetDisable(boolean value) Sets the value of thedisableproperty.final voidsetGraphic(Node value) Sets the value of thegraphicproperty.final voidSets the value of theidproperty.final voidsetMnemonicParsing(boolean value) Sets the value of themnemonicParsingproperty.final voidsetOnAction(EventHandler<ActionEvent> value) Sets the value of theonActionproperty.final voidsetOnMenuValidation(EventHandler<Event> value) Sets the value of theonMenuValidationproperty.protected final voidsetParentMenu(Menu value) Sets the value of theparentMenuproperty.protected final voidsetParentPopup(ContextMenu value) Sets the value of theparentPopupproperty.final voidSets the value of thestyleproperty.final voidSets the value of thetextproperty.voidsetUserData(Object value) Stores a user object that contains data at their discretion.final voidsetVisible(boolean value) Sets the value of thevisibleproperty.final StringPropertyA string representation of the CSS style associated with this specific MenuItem.final StringPropertyThe text to display in theMenuItem.final BooleanPropertySpecifies whether this MenuItem should be rendered as part of the scene graph.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods declared in interface EventTarget
addEventFilter, addEventHandler, removeEventFilter, removeEventHandlerModifier and TypeMethodDescriptiondefault <E extends Event>
voidaddEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Registers an event filter for this target.default <E extends Event>
voidaddEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Registers an event handler for this target.default <E extends Event>
voidremoveEventFilter(EventType<E> eventType, EventHandler<? super E> eventFilter) Unregisters a previously registered event filter from this target.default <E extends Event>
voidremoveEventHandler(EventType<E> eventType, EventHandler<? super E> eventHandler) Unregisters a previously registered event handler from this target.Methods declared in interface Styleable
getStyleableNodeModifier and TypeMethodDescriptiondefault NodeReturns the Node that represents this Styleable object.
-
Property Details
-
showing
Indicates whether theContextMenuis currently visible.- Default value:
- false
- See Also:
-
onShowing
Called just prior to theContextMenubeing 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.- See Also:
-
onShown
Called just after theContextMenuis shown.- See Also:
-
onHiding
Called just prior to theContextMenubeing hidden.- See Also:
-
onHidden
Called just after theContextMenuhas been hidden.- See Also:
-
-
Field Details
-
ON_SHOWING
-
ON_SHOWN
-
ON_HIDING
-
ON_HIDDEN
-
-
Constructor Details
-
Menu
public Menu()Constructs a Menu with an empty string for its display text.- Since:
- JavaFX 2.2
-
Menu
Constructs a Menu and sets the display text with the specified text.- Parameters:
text- the text to display on the menu button
-
Menu
-
Menu
Constructs a Menu and sets the display text with the specified text, the graphicNodeto the given node, and inserts the given items into theitemslist.- Parameters:
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.- Since:
- JavaFX 8u40
-
-
Method Details
-
isShowing
public final boolean isShowing()Gets the value of theshowingproperty.- Property description:
- Indicates whether the
ContextMenuis currently visible. - Default value:
- false
- Returns:
- the value of the
showingproperty - See Also:
-
showingProperty
Indicates whether theContextMenuis currently visible.- Default value:
- false
- Returns:
- the
showingproperty - See Also:
-
onShowingProperty
Called just prior to theContextMenubeing 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.- Returns:
- the on showing property
- See Also:
-
setOnShowing
Sets the value of theonShowingproperty.- Property description:
- Called just prior to the
ContextMenubeing 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. - Parameters:
value- the value for theonShowingproperty- See Also:
-
getOnShowing
Gets the value of theonShowingproperty.- Property description:
- Called just prior to the
ContextMenubeing 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. - Returns:
- the value of the
onShowingproperty - See Also:
-
onShownProperty
Called just after theContextMenuis shown.- Returns:
- the on shown property
- See Also:
-
setOnShown
Sets the value of theonShownproperty.- Property description:
- Called just after the
ContextMenuis shown. - Parameters:
value- the value for theonShownproperty- See Also:
-
getOnShown
Gets the value of theonShownproperty.- Property description:
- Called just after the
ContextMenuis shown. - Returns:
- the value of the
onShownproperty - See Also:
-
onHidingProperty
Called just prior to theContextMenubeing hidden.- Returns:
- the on hiding property
- See Also:
-
setOnHiding
Sets the value of theonHidingproperty.- Property description:
- Called just prior to the
ContextMenubeing hidden. - Parameters:
value- the value for theonHidingproperty- See Also:
-
getOnHiding
Gets the value of theonHidingproperty.- Property description:
- Called just prior to the
ContextMenubeing hidden. - Returns:
- the value of the
onHidingproperty - See Also:
-
onHiddenProperty
Called just after theContextMenuhas been hidden.- Returns:
- the on hidden property
- See Also:
-
setOnHidden
Sets the value of theonHiddenproperty.- Property description:
- Called just after the
ContextMenuhas been hidden. - Parameters:
value- the value for theonHiddenproperty- See Also:
-
getOnHidden
Gets the value of theonHiddenproperty.- Property description:
- Called just after the
ContextMenuhas been hidden. - Returns:
- the value of the
onHiddenproperty - See Also:
-
getItems
The items to show within this menu. If this ObservableList is modified at runtime, the Menu will update as expected.- Returns:
- the list of items
-
show
public void show()If the Menu is not disabled and theContextMenuis not already showing, then this will cause theContextMenuto be shown.- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
hide
public void hide()Hides theContextMenuif it was previously showing, and any showing submenus. If this menu is not showing, then invoking this function has no effect.- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-