APIdia Launch Partner
Top Description Fields Constructors Methods
javafx.scene.control

public Class Menu

extends MenuItem
Class Inheritance
Known Direct Subclasses
com.sun.javafx.scene.control.GlobalMenuAdapter
Annotations
@DefaultProperty:items
Imports
javafx.beans.DefaultProperty, javafx.beans.property.ObjectProperty, .ObjectPropertyBase, .ReadOnlyBooleanProperty, .ReadOnlyBooleanWrapper, javafx.collections.ListChangeListener.Change, .ObservableList, javafx.event.Event, .EventDispatchChain, .EventHandler, .EventType, javafx.scene.Node, com.sun.javafx.collections.TrackableObservableList, com.sun.javafx.scene.control.Logging, com.sun.javafx.tk.Toolkit

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);
Image of the Menu control

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
MenuBar, MenuItem

Field Summary

Modifier and TypeField and Description
private static final String
private final ObservableList<MenuItem>
public static final EventType<Event>
ON_HIDDEN

Called when the contextMenu for this menu is hidden.

public static final EventType<Event>
ON_HIDING

Called when the contextMenu for this menu will be hidden.

public static final EventType<Event>
ON_SHOWING

Called when the contextMenu for this menu will be shown.

public static final EventType<Event>
ON_SHOWN

Called when the contextMenu for this menu shows.

private ObjectProperty<EventHandler<Event>>
private ObjectProperty<EventHandler<Event>>
private ObjectProperty<EventHandler<Event>>
private ObjectProperty<EventHandler<Event>>
private ReadOnlyBooleanWrapper
showing

Indicates whether the ContextMenu is currently visible.

private static final String
Inherited from javafx.scene.control.MenuItem:
eventHandlerManagerMENU_VALIDATION_EVENT

Constructor Summary

AccessConstructor and Description

Method Summary

Modifier and TypeMethod and Description
public EventDispatchChain
public final ObservableList<MenuItem>

Returns:

the list of items
getItems
()

The items to show within this menu.

public final EventHandler<Event>
public final EventHandler<Event>
public final EventHandler<Event>
public final EventHandler<Event>
public void
hide()

Hides the ContextMenu if it was previously showing, and any showing submenus.

public final boolean
public final ObjectProperty<EventHandler<Event>>

Returns:

the on hidden property
onHiddenProperty
()

Called just after the ContextMenu has been hidden.

public final ObjectProperty<EventHandler<Event>>

Returns:

the on hiding property
onHidingProperty
()

Called just prior to the ContextMenu being hidden.

public final ObjectProperty<EventHandler<Event>>

Returns:

the on showing property
onShowingProperty
()

Called just prior to the ContextMenu being shown, even if the menu has no items to show.

public final ObjectProperty<EventHandler<Event>>

Returns:

the on shown property
onShownProperty
()

Called just after the ContextMenu is shown.

public final void
public final void
public final void
public final void
private void
setShowing(boolean value)

public void
show()

If the Menu is not disabled and the ContextMenu is not already showing, then this will cause the ContextMenu to be shown.

public final ReadOnlyBooleanProperty
private ReadOnlyBooleanWrapper
Inherited from javafx.scene.control.MenuItem:
acceleratorPropertyaddEventFilteraddEventHandlerdisablePropertyfiregetAcceleratorgetCssMetaDatagetGraphicgetIdgetOnActiongetOnMenuValidationgetParentMenugetParentPopupgetPropertiesgetPseudoClassStatesgetStylegetStyleableNodegetStyleableParentgetStyleClassgetTextgetTypeSelectorgetUserDatagraphicPropertyidPropertyisDisableisMnemonicParsingisVisiblemnemonicParsingPropertyonActionPropertyonMenuValidationPropertyparentMenuPropertyparentPopupPropertyremoveEventFilterremoveEventHandlersetAcceleratorsetDisablesetGraphicsetIdsetMnemonicParsingsetOnActionsetOnMenuValidationsetParentMenusetParentPopupsetStylesetTextsetUserDatasetVisiblestylePropertytextPropertytoStringvisibleProperty

Field Detail

DEFAULT_STYLE_CLASSback to summary
private static final String DEFAULT_STYLE_CLASS

Hides javafx.scene.control.MenuItem.DEFAULT_STYLE_CLASS.

itemsback to summary
private final ObservableList<MenuItem> items
ON_HIDDENback to summary
public static final EventType<Event> ON_HIDDEN

Called when the contextMenu for this menu is hidden. However if the contextMenu is empty then this will not be called.

ON_HIDINGback to summary
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.

ON_SHOWINGback to summary
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.

ON_SHOWNback to summary
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.

onHiddenback to summary
private ObjectProperty<EventHandler<Event>> onHidden
onHidingback to summary
private ObjectProperty<EventHandler<Event>> onHiding
onShowingback to summary
private ObjectProperty<EventHandler<Event>> onShowing
onShownback to summary
private ObjectProperty<EventHandler<Event>> onShown
showingback to summary
private ReadOnlyBooleanWrapper showing

Indicates whether the ContextMenu is currently visible.

Default Value
false
STYLE_CLASS_SHOWINGback to summary
private static final String STYLE_CLASS_SHOWING

Constructor Detail

Method Detail

buildEventDispatchChainback to summary
public EventDispatchChain buildEventDispatchChain(EventDispatchChain tail)

Overrides javafx.scene.control.MenuItem.buildEventDispatchChain.

Implements javafx.event.EventTarget.buildEventDispatchChain.

Doc from javafx.event.EventTarget.buildEventDispatchChain.

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 this 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.

Annotations
@Override
getItemsback to summary
public final ObservableList<MenuItem> getItems()

The items to show within this menu. If this ObservableList is modified at runtime, the Menu will update as expected.

Returns:ObservableList<MenuItem>

the list of items

getOnHiddenback to summary
public final EventHandler<Event> getOnHidden()
getOnHidingback to summary
public final EventHandler<Event> getOnHiding()
getOnShowingback to summary
public final EventHandler<Event> getOnShowing()
getOnShownback to summary
public final EventHandler<Event> getOnShown()
hideback to summary
public void hide()

Hides the ContextMenu if it was previously showing, and any showing submenus. If this menu is not showing, then invoking this function has no effect.

Exceptions
IllegalStateException:
if this method is called on a thread other than the JavaFX Application Thread.
isShowingback to summary
public final boolean isShowing()
onHiddenPropertyback to summary
public final ObjectProperty<EventHandler<Event>> onHiddenProperty()

Called just after the ContextMenu has been hidden.

Returns:ObjectProperty<EventHandler<Event>>

the on hidden property

onHidingPropertyback to summary
public final ObjectProperty<EventHandler<Event>> onHidingProperty()

Called just prior to the ContextMenu being hidden.

Returns:ObjectProperty<EventHandler<Event>>

the on hiding property

onShowingPropertyback to summary
public final ObjectProperty<EventHandler<Event>> onShowingProperty()

Called just prior to the 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.

Returns:ObjectProperty<EventHandler<Event>>

the on showing property

onShownPropertyback to summary
public final ObjectProperty<EventHandler<Event>> onShownProperty()

Called just after the ContextMenu is shown.

Returns:ObjectProperty<EventHandler<Event>>

the on shown property

setOnHiddenback to summary
public final void setOnHidden(EventHandler<Event> value)
setOnHidingback to summary
public final void setOnHiding(EventHandler<Event> value)
setOnShowingback to summary
public final void setOnShowing(EventHandler<Event> value)
setOnShownback to summary
public final void setOnShown(EventHandler<Event> value)
setShowingback to summary
private void setShowing(boolean value)
showback to summary
public void show()

If the Menu is not disabled and the ContextMenu is not already showing, then this will cause the ContextMenu to be shown.

Exceptions
IllegalStateException:
if this method is called on a thread other than the JavaFX Application Thread.
showingPropertyback to summary
public final ReadOnlyBooleanProperty showingProperty()
showingPropertyImplback to summary
private ReadOnlyBooleanWrapper showingPropertyImpl()