Class ContextMenu
- All Implemented Interfaces:
Styleable, EventTarget, Skinnable
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);
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:
-
Nested Class Summary
Nested classes/interfaces declared in class PopupControl
PopupControl.CSSBridgeModifier and TypeClassDescriptionprotected classThe link between the popup window and the scenegraph.Nested classes/interfaces declared in class PopupWindow
PopupWindow.AnchorLocationModifier and TypeClassDescriptionstatic enumAnchor location constants for popup anchor point selection. -
Property Summary
PropertiesTypePropertyDescriptionfinal ObjectProperty<EventHandler<ActionEvent>> Callback function to be informed when an item contained within thisContextMenuhas been activated.Properties declared in class PopupControl
id, maxHeight, maxWidth, minHeight, minWidth, prefHeight, prefWidth, skin, styleTypePropertyDescriptionfinal StringPropertyThe id of thisPopupControl.final DoublePropertyProperty for overriding the control's computed maximum height.final DoublePropertyProperty for overriding the control's computed maximum width.final DoublePropertyProperty for overriding the control's computed minimum height.final DoublePropertyProperty for overriding the control's computed minimum width.final DoublePropertyProperty for overriding the control's computed preferred height.final DoublePropertyProperty for overriding the control's computed preferred width.final ObjectProperty<Skin<?>> Skin is responsible for rendering thisPopupControl.final StringPropertyA string representation of the CSS style associated with this specificPopupControl.Properties declared in class PopupWindow
anchorLocation, anchorX, anchorY, autoFix, autoHide, consumeAutoHidingEvents, hideOnEscape, onAutoHide, ownerNode, ownerWindowTypePropertyDescriptionSpecifies the popup anchor point which is used in popup positioning.final ReadOnlyDoublePropertySpecifies the x coordinate of the popup anchor point on the screen.final ReadOnlyDoublePropertySpecifies the y coordinate of the popup anchor point on the screen.final BooleanPropertyThis convenience variable indicates whether, when the popup is shown, it should automatically correct its position such that it doesn't end up positioned off the screen.final BooleanPropertySpecifies whether Popups should auto hide.final BooleanPropertySpecifies whether the event, which caused the Popup to hide, should be consumed.final BooleanPropertySpecifies whether the PopupWindow should be hidden when an unhandled escape key is pressed while the popup has focus.final ObjectProperty<EventHandler<Event>> Called after autoHide is run.final ReadOnlyObjectProperty<Node> The node which is the owner of this popup.final ReadOnlyObjectProperty<Window> The window which is the parent of this popup.Properties declared in class Window
eventDispatcher, focused, forceIntegerRenderScale, height, onCloseRequest, onHidden, onHiding, onShowing, onShown, opacity, outputScaleX, outputScaleY, renderScaleX, renderScaleY, scene, showing, width, x, yTypePropertyDescriptionfinal ObjectProperty<EventDispatcher> Specifies the event dispatcher for this node.final ReadOnlyBooleanPropertyWhether or not thisWindowhas the keyboard or input focus.final BooleanPropertyBoolean property that controls whether only integer render scales are set by default by the system when there is a change in the associated output scale.final ReadOnlyDoublePropertyThe height of thisWindow.final ObjectProperty<EventHandler<WindowEvent>> Called when there is an external request to close thisWindow.final ObjectProperty<EventHandler<WindowEvent>> Called just after the Window has been hidden.final ObjectProperty<EventHandler<WindowEvent>> Called just prior to the Window being hidden.final ObjectProperty<EventHandler<WindowEvent>> Called just prior to the Window being shown.final ObjectProperty<EventHandler<WindowEvent>> Called just after the Window is shown.final DoublePropertyDefines the opacity of theWindowas a value between 0.0 and 1.0.final ReadOnlyDoublePropertyThe scale that theWindowwill apply to horizontal scene coordinates in all stages of rendering and compositing the output to the screen or other destination device.final ReadOnlyDoublePropertyThe scale that theWindowwill apply to vertical scene coordinates in all stages of rendering and compositing the output to the screen or other destination device.final DoublePropertyThe horizontal scale that theWindowwill use when rendering itsSceneto the rendering buffer.final DoublePropertyThe vertical scale that theWindowwill use when rendering itsSceneto the rendering buffer.final ReadOnlyObjectProperty<Scene> TheSceneto be rendered on thisWindow.final ReadOnlyBooleanPropertyWhether or not thisWindowis showing (that is, open on the user's system).final ReadOnlyDoublePropertyThe width of thisWindow.final ReadOnlyDoublePropertyThe horizontal location of thisWindowon the screen.final ReadOnlyDoublePropertyThe vertical location of thisWindowon the screen. -
Field Summary
Fields declared in class PopupControl
bridge, USE_COMPUTED_SIZE, USE_PREF_SIZEModifier and TypeFieldDescriptionprotected PopupControl.CSSBridgeWe need a special root node, except we can't replace the special root node already in the PopupControl.static final doubleSentinel value which can be passed to a control's setMinWidth(), setMinHeight(), setPrefWidth(), setPrefHeight(), setMaxWidth(), setMaxHeight() methods to reset the control's size constraint back to it's intrinsic size returned by computeMinWidth(), computeMinHeight(), computePrefWidth(), computePrefHeight(), computeMaxWidth(), or computeMaxHeight().static final doubleSentinel value which can be passed to a control's setMinWidth(), setMinHeight(), setMaxWidth() or setMaxHeight() methods to indicate that the preferred dimension should be used for that max and/or min constraint. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new ContextMenuContextMenu(MenuItem... items) Create a new ContextMenu initialized with the given items -
Method Summary
Modifier and TypeMethodDescriptionfinal ObservableList<MenuItem> getItems()The menu items on the context menu.final EventHandler<ActionEvent> Gets the value of theonActionproperty.voidhide()Hides thisContextMenuand any visible submenus, assuming that when this function is called that theContextMenuwas showing.final ObjectProperty<EventHandler<ActionEvent>> Callback function to be informed when an item contained within thisContextMenuhas been activated.final voidsetOnAction(EventHandler<ActionEvent> value) Sets the value of theonActionproperty.voidShows theContextMenuat the specified screen coordinates.voidShows theContextMenurelative to the given anchor node, on the side specified by thesideparameter, and offset by the givendxanddyvalues for the x-axis and y-axis, respectively.Methods declared in class PopupControl
createDefaultSkin, getClassCssMetaData, getCssMetaData, getId, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getPrefHeight, getPrefWidth, getPseudoClassStates, getSkin, getStyle, getStyleableParent, getStyleClass, getTypeSelector, idProperty, maxHeight, maxHeightProperty, maxWidth, maxWidthProperty, minHeight, minHeightProperty, minWidth, minWidthProperty, prefHeight, prefHeightProperty, prefWidth, prefWidthProperty, pseudoClassStateChanged, setId, setMaxHeight, setMaxSize, setMaxWidth, setMinHeight, setMinSize, setMinWidth, setPrefHeight, setPrefSize, setPrefWidth, setSkin, setStyle, skinProperty, stylePropertyModifier and TypeMethodDescriptionprotected Skin<?> Create a new instance of the default skin for this control.static List<CssMetaData<? extends Styleable, ?>> Gets theCssMetaDataassociated with this class, which may include theCssMetaDataof its superclasses.List<CssMetaData<? extends Styleable, ?>> The CssMetaData of this Styleable.final StringgetId()The id of thisPopupControl.final doubleProperty for overriding the control's computed maximum height.final doubleProperty for overriding the control's computed maximum width.final doubleProperty for overriding the control's computed minimum height.final doubleProperty for overriding the control's computed minimum width.final doubleProperty for overriding the control's computed preferred height.final doubleProperty for overriding the control's computed preferred width.final ObservableSet<PseudoClass> Return the pseudo-class state of this Styleable.final Skin<?> getSkin()Gets the value of theskinproperty.final StringgetStyle()Gets the value of thestyleproperty.Return the parent of this Styleable, or null if there is no parent.final ObservableList<String> Returns the list of String identifiers that make up the styleClass for this PopupControl.The type of thisStyleablethat is to be used in selector matching.final StringPropertyThe id of thisPopupControl.final doublemaxHeight(double width) Called during layout to determine the maximum height for this node.final DoublePropertyProperty for overriding the control's computed maximum height.final doublemaxWidth(double height) Called during layout to determine the maximum width for this node.final DoublePropertyProperty for overriding the control's computed maximum width.final doubleminHeight(double width) Called during layout to determine the minimum height for this node.final DoublePropertyProperty for overriding the control's computed minimum height.final doubleminWidth(double height) Called during layout to determine the minimum width for this node.final DoublePropertyProperty for overriding the control's computed minimum width.final doubleprefHeight(double width) Called during layout to determine the preferred height for this node.final DoublePropertyProperty for overriding the control's computed preferred height.final doubleprefWidth(double height) Called during layout to determine the preferred width for this node.final DoublePropertyProperty for overriding the control's computed preferred width.final voidpseudoClassStateChanged(PseudoClass pseudoClass, boolean active) Used to specify that a pseudo-class of this node has changed.final voidSets the id of thisPopupControl.final voidsetMaxHeight(double value) Property for overriding the control's computed maximum height.voidsetMaxSize(double maxWidth, double maxHeight) Convenience method for overriding the control's computed maximum width and height.final voidsetMaxWidth(double value) Property for overriding the control's computed maximum width.final voidsetMinHeight(double value) Property for overriding the control's computed minimum height.voidsetMinSize(double minWidth, double minHeight) Convenience method for overriding the control's computed minimum width and height.final voidsetMinWidth(double value) Property for overriding the control's computed minimum width.final voidsetPrefHeight(double value) Property for overriding the control's computed preferred height.voidsetPrefSize(double prefWidth, double prefHeight) Convenience method for overriding the control's computed preferred width and height.final voidsetPrefWidth(double value) Property for overriding the control's computed preferred width.final voidSets the value of theskinproperty.final voidSets the value of thestyleproperty.final ObjectProperty<Skin<?>> Skin is responsible for rendering thisPopupControl.final StringPropertyA string representation of the CSS style associated with this specificPopupControl.Methods declared in class PopupWindow
anchorLocationProperty, anchorXProperty, anchorYProperty, autoFixProperty, autoHideProperty, consumeAutoHidingEventsProperty, getAnchorLocation, getAnchorX, getAnchorY, getConsumeAutoHidingEvents, getOnAutoHide, getOwnerNode, getOwnerWindow, hideOnEscapeProperty, isAutoFix, isAutoHide, isHideOnEscape, onAutoHideProperty, ownerNodeProperty, ownerWindowProperty, setAnchorLocation, setAnchorX, setAnchorY, setAutoFix, setAutoHide, setConsumeAutoHidingEvents, setHideOnEscape, setOnAutoHide, setScene, show, showModifier and TypeMethodDescriptionSpecifies the popup anchor point which is used in popup positioning.final ReadOnlyDoublePropertySpecifies the x coordinate of the popup anchor point on the screen.final ReadOnlyDoublePropertySpecifies the y coordinate of the popup anchor point on the screen.final BooleanPropertyThis convenience variable indicates whether, when the popup is shown, it should automatically correct its position such that it doesn't end up positioned off the screen.final BooleanPropertySpecifies whether Popups should auto hide.final BooleanPropertySpecifies whether the event, which caused the Popup to hide, should be consumed.Gets the value of theanchorLocationproperty.final doubleGets the value of theanchorXproperty.final doubleGets the value of theanchorYproperty.final booleanGets the value of theconsumeAutoHidingEventsproperty.final EventHandler<Event> Gets the value of theonAutoHideproperty.final NodeGets the value of theownerNodeproperty.final WindowGets the value of theownerWindowproperty.final BooleanPropertySpecifies whether the PopupWindow should be hidden when an unhandled escape key is pressed while the popup has focus.final booleanGets the value of theautoFixproperty.final booleanGets the value of theautoHideproperty.final booleanGets the value of thehideOnEscapeproperty.final ObjectProperty<EventHandler<Event>> Called after autoHide is run.final ReadOnlyObjectProperty<Node> The node which is the owner of this popup.final ReadOnlyObjectProperty<Window> The window which is the parent of this popup.final voidSets the value of theanchorLocationproperty.final voidsetAnchorX(double value) Sets the value of theanchorXproperty.final voidsetAnchorY(double value) Sets the value of theanchorYproperty.final voidsetAutoFix(boolean value) Sets the value of theautoFixproperty.final voidsetAutoHide(boolean value) Sets the value of theautoHideproperty.final voidsetConsumeAutoHidingEvents(boolean value) Sets the value of theconsumeAutoHidingEventsproperty.final voidsetHideOnEscape(boolean value) Sets the value of thehideOnEscapeproperty.final voidsetOnAutoHide(EventHandler<Event> value) Sets the value of theonAutoHideproperty.protected final voidNote to subclasses: the scene used by PopupWindow is very specifically managed by PopupWindow.voidShow the popup.voidShows the popup at the specified location on the screen.Methods declared in class Window
addEventFilter, addEventHandler, buildEventDispatchChain, centerOnScreen, eventDispatcherProperty, fireEvent, focusedProperty, forceIntegerRenderScaleProperty, getEventDispatcher, getHeight, getOnCloseRequest, getOnHidden, getOnHiding, getOnShowing, getOnShown, getOpacity, getOutputScaleX, getOutputScaleY, getProperties, getRenderScaleX, getRenderScaleY, getScene, getUserData, getWidth, getWindows, getX, getY, hasProperties, heightProperty, isFocused, isForceIntegerRenderScale, isShowing, onCloseRequestProperty, onHiddenProperty, onHidingProperty, onShowingProperty, onShownProperty, opacityProperty, outputScaleXProperty, outputScaleYProperty, removeEventFilter, removeEventHandler, renderScaleXProperty, renderScaleYProperty, requestFocus, sceneProperty, setEventDispatcher, setEventHandler, setForceIntegerRenderScale, setHeight, setOnCloseRequest, setOnHidden, setOnHiding, setOnShowing, setOnShown, setOpacity, setRenderScaleX, setRenderScaleY, setUserData, setWidth, setX, setY, show, showingProperty, sizeToScene, widthProperty, xProperty, yPropertyModifier and TypeMethodDescriptionfinal <T extends Event>
voidaddEventFilter(EventType<T> eventType, EventHandler<? super T> eventFilter) Registers an event filter for this target.final <T extends Event>
voidaddEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler) Registers an event handler for this target.Construct an event dispatch chain for this target.voidSets x and y properties on this Window so that it is centered on the current screen.final ObjectProperty<EventDispatcher> Specifies the event dispatcher for this node.final voidFires the specified event.final ReadOnlyBooleanPropertyWhether or not thisWindowhas the keyboard or input focus.final BooleanPropertyBoolean property that controls whether only integer render scales are set by default by the system when there is a change in the associated output scale.final EventDispatcherGets the value of theeventDispatcherproperty.final doubleGets the value of theheightproperty.final EventHandler<WindowEvent> Gets the value of theonCloseRequestproperty.final EventHandler<WindowEvent> Gets the value of theonHiddenproperty.final EventHandler<WindowEvent> Gets the value of theonHidingproperty.final EventHandler<WindowEvent> Gets the value of theonShowingproperty.final EventHandler<WindowEvent> Gets the value of theonShownproperty.final doubleGets the value of theopacityproperty.final doubleGets the value of theoutputScaleXproperty.final doubleGets the value of theoutputScaleYproperty.final ObservableMap<Object, Object> Returns an observable map of properties on this node for use primarily by application developers.final doubleGets the value of therenderScaleXproperty.final doubleGets the value of therenderScaleYproperty.final ScenegetScene()Gets the value of thesceneproperty.Returns a previously set Object property, or null if no such property has been set using theWindow.setUserData(java.lang.Object)method.final doublegetWidth()Gets the value of thewidthproperty.static ObservableList<Window> Returns a list containing a reference to the currently showing JavaFX windows.final doublegetX()Gets the value of thexproperty.final doublegetY()Gets the value of theyproperty.booleanTests if Window has properties.final ReadOnlyDoublePropertyThe height of thisWindow.final booleanGets the value of thefocusedproperty.final booleanGets the value of theforceIntegerRenderScaleproperty.final booleanGets the value of theshowingproperty.final ObjectProperty<EventHandler<WindowEvent>> Called when there is an external request to close thisWindow.final ObjectProperty<EventHandler<WindowEvent>> Called just after the Window has been hidden.final ObjectProperty<EventHandler<WindowEvent>> Called just prior to the Window being hidden.final ObjectProperty<EventHandler<WindowEvent>> Called just prior to the Window being shown.final ObjectProperty<EventHandler<WindowEvent>> Called just after the Window is shown.final DoublePropertyDefines the opacity of theWindowas a value between 0.0 and 1.0.final ReadOnlyDoublePropertyThe scale that theWindowwill apply to horizontal scene coordinates in all stages of rendering and compositing the output to the screen or other destination device.final ReadOnlyDoublePropertyThe scale that theWindowwill apply to vertical scene coordinates in all stages of rendering and compositing the output to the screen or other destination device.final <T extends Event>
voidremoveEventFilter(EventType<T> eventType, EventHandler<? super T> eventFilter) Unregisters a previously registered event filter from this target.final <T extends Event>
voidremoveEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler) Unregisters a previously registered event handler from this target.final DoublePropertyThe horizontal scale that theWindowwill use when rendering itsSceneto the rendering buffer.final DoublePropertyThe vertical scale that theWindowwill use when rendering itsSceneto the rendering buffer.final voidRequests that thisWindowget the input focus.final ReadOnlyObjectProperty<Scene> TheSceneto be rendered on thisWindow.final voidSets the value of theeventDispatcherproperty.protected final <T extends Event>
voidsetEventHandler(EventType<T> eventType, EventHandler<? super T> eventHandler) Sets the handler to use for this event type.final voidsetForceIntegerRenderScale(boolean forced) Sets the value of theforceIntegerRenderScaleproperty.final voidsetHeight(double value) Sets the value of theheightproperty.final voidsetOnCloseRequest(EventHandler<WindowEvent> value) Sets the value of theonCloseRequestproperty.final voidsetOnHidden(EventHandler<WindowEvent> value) Sets the value of theonHiddenproperty.final voidsetOnHiding(EventHandler<WindowEvent> value) Sets the value of theonHidingproperty.final voidsetOnShowing(EventHandler<WindowEvent> value) Sets the value of theonShowingproperty.final voidsetOnShown(EventHandler<WindowEvent> value) Sets the value of theonShownproperty.final voidsetOpacity(double value) Sets the value of theopacityproperty.final voidsetRenderScaleX(double scale) Sets the value of therenderScaleXproperty.final voidsetRenderScaleY(double scale) Sets the value of therenderScaleYproperty.voidsetUserData(Object value) Convenience method for setting a single Object property that can be retrieved at a later date.final voidsetWidth(double value) Sets the value of thewidthproperty.final voidsetX(double value) Sets the value of thexproperty.final voidsetY(double value) Sets the value of theyproperty.protected voidshow()Attempts to show this Window by setting visibility to truefinal ReadOnlyBooleanPropertyWhether or not thisWindowis showing (that is, open on the user's system).voidSet the width and height of this Window to match the size of the content of this Window's Scene.final ReadOnlyDoublePropertyThe width of thisWindow.final ReadOnlyDoublePropertyThe horizontal location of thisWindowon the screen.final ReadOnlyDoublePropertyThe vertical location of thisWindowon the screen.Methods declared in class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods declared in interface Styleable
getStyleableNodeModifier and TypeMethodDescriptiondefault NodeReturns the Node that represents this Styleable object.
-
Property Details
-
onAction
Callback function to be informed when an item contained within thisContextMenuhas 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.- See Also:
-
-
Constructor Details
-
ContextMenu
public ContextMenu()Create a new ContextMenu -
ContextMenu
Create a new ContextMenu initialized with the given items- Parameters:
items- the list of menu items
-
-
Method Details
-
setOnAction
Sets the value of theonActionproperty.- Property description:
- Callback function to be informed when an item contained within this
ContextMenuhas 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. - Parameters:
value- the value for theonActionproperty- See Also:
-
getOnAction
Gets the value of theonActionproperty.- Property description:
- Callback function to be informed when an item contained within this
ContextMenuhas 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. - Returns:
- the value of the
onActionproperty - See Also:
-
onActionProperty
Callback function to be informed when an item contained within thisContextMenuhas 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.- Returns:
- the
onActionproperty - See Also:
-
getItems
The menu items on the context menu. If this ObservableList is modified at runtime, the ContextMenu will update as expected.- Returns:
- the menu items on this context menu
- See Also:
-
show
Shows theContextMenurelative to the given anchor node, on the side specified by thesideparameter, and offset by the givendxanddyvalues 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
sideparameter, consider that it is relative to the anchor node. As such, asideofTOPwould 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- the anchor nodeside- the sidedx- the dx value for the x-axisdy- the dy value for the y-axis- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
show
Shows theContextMenuat the specified screen coordinates. If there is not enough room at the specified location to show theContextMenugiven its size requirements, the necessary adjustments are made to bring theContextMenuback on screen. This also means that theContextMenuwill not span multiple monitors.- Overrides:
showin classPopupWindow- Parameters:
anchor- the anchor nodescreenX- the x position of the anchor in screen coordinatesscreenY- the y position of the anchor in screen coordinates- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-
hide
public void hide()Hides thisContextMenuand any visible submenus, assuming that when this function is called that theContextMenuwas showing.If this
ContextMenuis not showing, then nothing happens.- Overrides:
hidein classPopupWindow- Throws:
IllegalStateException- if this method is called on a thread other than the JavaFX Application Thread.
-