Top Description Fields Constructors Methods
javafx.scene.control

public Class ContextMenu

extends PopupControl
Class Inheritance
Annotations
@IDProperty:id
Imports
javafx.beans.property.ObjectProperty, .ObjectPropertyBase, javafx.collections.ListChangeListener.Change, .ObservableList, javafx.event.ActionEvent, .Event, .EventHandler, javafx.geometry.HPos, .Point2D, .Side, .VPos, javafx.scene.Node, .Scene, javafx.scene.control.skin.ContextMenuSkin, javafx.stage.Window, com.sun.javafx.beans.IDProperty, com.sun.javafx.collections.TrackableObservableList, com.sun.javafx.tk.Toolkit, com.sun.javafx.util.Utils

A popup control containing an ObservableList of menu items. The items ObservableList allows for any MenuItem type to be inserted, including its subclasses Menu, MenuItem, RadioMenuItem, CheckMenuItem and CustomMenuItem. If an arbitrary Node needs to be inserted into a menu, a CustomMenuItem can be used. One exception to this general rule is that SeparatorMenuItem could be used for inserting a separator.

A common use case for this class is creating and showing context menus to users. To create a context menu using ContextMenu you can do the following:

final ContextMenu contextMenu = new ContextMenu();
contextMenu.setOnShowing(new EventHandler<WindowEvent>() {
    public void handle(WindowEvent e) {
        System.out.println("showing");
    }
});
contextMenu.setOnShown(new EventHandler<WindowEvent>() {
    public void handle(WindowEvent e) {
        System.out.println("shown");
    }
});

MenuItem item1 = new MenuItem("About");
item1.setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e) {
        System.out.println("About");
    }
});
MenuItem item2 = new MenuItem("Preferences");
item2.setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e) {
        System.out.println("Preferences");
    }
});
contextMenu.getItems().addAll(item1, item2);

final TextField textField = new TextField("Type Something");
textField.setContextMenu(contextMenu);
Image of the ContextMenu control

Control#setContextMenu(javafx.scene.control.ContextMenu) convenience method can be used to set a context menu on on any control. The example above results in the context menu being displayed on the right Side of the TextField. Alternatively, an event handler can also be set on the control to invoke the context menu as shown below.

textField.setOnAction(new EventHandler<ActionEvent>() {
    public void handle(ActionEvent e) {
        contextMenu.show(textField, Side.BOTTOM, 0, 0);
    }
});

Group root = (Group) scene.getRoot();
root.getChildren().add(textField);

In this example, the context menu is shown when the user clicks on the Button (of course, you should use the MenuButton control to do this rather than doing the above).

Note that the show function used in the code sample above will result in the ContextMenu appearing directly beneath the TextField. You can vary the Side to get the results you expect.

Since
JavaFX 2.0
See Also
MenuItem, Menu

Field Summary

Modifier and TypeField and Description
private static final String
private final ObservableList<MenuItem>
private ObjectProperty<EventHandler<ActionEvent>>
onAction

Callback function to be informed when an item contained within this ContextMenu has been activated.

private boolean
Inherited from javafx.scene.control.PopupControl:
bridgeUSE_COMPUTED_SIZEUSE_PREF_SIZE

Constructor Summary

AccessConstructor and Description
public
ContextMenu()

Create a new ContextMenu

public
ContextMenu(MenuItem...
the list of menu items
items
)

Create a new ContextMenu initialized with the given items

Method Summary

Modifier and TypeMethod and Description
protected Skin<?>
createDefaultSkin()

Overrides javafx.scene.control.PopupControl.createDefaultSkin.

Create a new instance of the default skin for this control.

private void
doShow(Node anchor, double screenX, double screenY)

public final ObservableList<MenuItem>

Returns:

the menu items on this context menu
getItems
()

The menu items on the context menu.

public final EventHandler<ActionEvent>
public void
hide()

Overrides javafx.stage.PopupWindow.hide.

Hides this ContextMenu and any visible submenus, assuming that when this function is called that the ContextMenu was showing.

pack-priv final boolean
public final ObjectProperty<EventHandler<ActionEvent>>
public final void
pack-priv final void
setShowRelativeToWindow(boolean value)

public void
show(Node
the anchor node
anchor
,
Side
the side
side
,
double
the dx value for the x-axis
dx
,
double
the dy value for the y-axis
dy
)

Shows the ContextMenu relative to the given anchor node, on the side specified by the side parameter, and offset by the given dx and dy values for the x-axis and y-axis, respectively.

public void
show(Node
the anchor node
anchor
,
double
the x position of the anchor in screen coordinates
screenX
,
double
the y position of the anchor in screen coordinates
screenY
)

Overrides javafx.stage.PopupWindow.show.

Shows the ContextMenu at the specified screen coordinates.

Inherited from javafx.scene.control.PopupControl:
getClassCssMetaDatagetCssMetaDatagetIdgetMaxHeightgetMaxWidthgetMinHeightgetMinWidthgetPrefHeightgetPrefWidthgetPseudoClassStatesgetSkingetStylegetStyleableNodegetStyleableParentgetStyleClassgetTypeSelectoridPropertymaxHeightmaxHeightPropertymaxWidthmaxWidthPropertyminHeightminHeightPropertyminWidthminWidthPropertyprefHeightprefHeightPropertyprefWidthprefWidthPropertypseudoClassStateChangedsetIdsetMaxHeightsetMaxSizesetMaxWidthsetMinHeightsetMinSizesetMinWidthsetPrefHeightsetPrefSizesetPrefWidthsetSkinsetStyleskinPropertystyleProperty

Field Detail

DEFAULT_STYLE_CLASSback to summary
private static final String DEFAULT_STYLE_CLASS
itemsback to summary
private final ObservableList<MenuItem> items
onActionback to summary
private ObjectProperty<EventHandler<ActionEvent>> onAction

Callback function to be informed when an item contained within this ContextMenu has been activated. The current implementation informs all parent menus as well, so that it is not necessary to listen to all sub menus for events.

showRelativeToWindowback to summary
private boolean showRelativeToWindow

Constructor Detail

ContextMenuback to summary
public ContextMenu()

Create a new ContextMenu

ContextMenuback to summary
public ContextMenu(MenuItem... items)

Create a new ContextMenu initialized with the given items

Parameters
items:MenuItem[]

the list of menu items

Method Detail

createDefaultSkinback to summary
protected Skin<?> createDefaultSkin()

Overrides javafx.scene.control.PopupControl.createDefaultSkin.

Doc from javafx.scene.control.PopupControl.createDefaultSkin.

Create a new instance of the default skin for this control. This is called to create a skin for the control if no skin is provided via CSS -fx-skin or set explicitly in a sub-class with setSkin(...).

Annotations
@Override
doShowback to summary
private void doShow(Node anchor, double screenX, double screenY)
getItemsback to summary
public final ObservableList<MenuItem> getItems()

The menu items on the context menu. If this ObservableList is modified at runtime, the ContextMenu will update as expected.

Returns:ObservableList<MenuItem>

the menu items on this context menu

See Also
MenuItem
getOnActionback to summary
public final EventHandler<ActionEvent> getOnAction()
hideback to summary
public void hide()

Overrides javafx.stage.PopupWindow.hide.

Hides this ContextMenu and any visible submenus, assuming that when this function is called that the ContextMenu was showing.

If this ContextMenu is not showing, then nothing happens.

Annotations
@Override
Exceptions
IllegalStateException:
if this method is called on a thread other than the JavaFX Application Thread.
isShowRelativeToWindowback to summary
pack-priv final boolean isShowRelativeToWindow()
onActionPropertyback to summary
public final ObjectProperty<EventHandler<ActionEvent>> onActionProperty()
setOnActionback to summary
public final void setOnAction(EventHandler<ActionEvent> value)
setShowRelativeToWindowback to summary
pack-priv final void setShowRelativeToWindow(boolean value)
showback to summary
public void show(Node anchor, Side side, double dx, double dy)

Shows the ContextMenu relative to the given anchor node, on the side specified by the side parameter, and offset by the given dx and dy values for the x-axis and y-axis, respectively. If there is not enough room, the menu is moved to the opposite side and the offset is not applied.

To clarify the purpose of the side parameter, consider that it is relative to the anchor node. As such, a side of TOP would mean that the ContextMenu's bottom left corner is set to the top left corner of the anchor.

This function is useful for finely tuning the position of a menu, relative to the parent node to ensure close alignment.

Parameters
anchor:Node

the anchor node

side:Side

the side

dx:double

the dx value for the x-axis

dy:double

the dy value for the y-axis

Exceptions
IllegalStateException:
if this method is called on a thread other than the JavaFX Application Thread.
showback to summary
public void show(Node anchor, double screenX, double screenY)

Overrides javafx.stage.PopupWindow.show.

Shows the ContextMenu at the specified screen coordinates. If there is not enough room at the specified location to show the ContextMenu given its size requirements, the necessary adjustments are made to bring the ContextMenu back on screen. This also means that the ContextMenu will not span multiple monitors.

Parameters
anchor:Node

the anchor node

screenX:double

the x position of the anchor in screen coordinates

screenY:double

the y position of the anchor in screen coordinates

Annotations
@Override
Exceptions
IllegalStateException:
if this method is called on a thread other than the JavaFX Application Thread.