public class ContextMenu extends PopupControl
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.
Type | Property and Description |
---|---|
<any> |
onAction
Callback function to be informed when an item contained within this
ContextMenu has been activated. |
idProperty, maxHeightProperty, maxWidthProperty, minHeightProperty, minWidthProperty, prefHeightProperty, prefWidthProperty, skinProperty, styleProperty
PopupControl.CSSBridge, PopupControl.CSSBridgeHelper
Modifier and Type | Field and Description |
---|---|
private static java.lang.String |
DEFAULT_STYLE_CLASS
*
Stylesheet Handling *
*
|
private <any> |
items |
private <any> |
onAction
Callback function to be informed when an item contained within this
ContextMenu has been activated. |
private boolean |
showRelativeToWindow
*
Fields *
*
|
bridge, USE_COMPUTED_SIZE, USE_PREF_SIZE
Constructor and Description |
---|
ContextMenu()
Create a new ContextMenu
|
ContextMenu(MenuItem... items)
Create a new ContextMenu initialized with the given items
|
Modifier and Type | Method and Description |
---|---|
protected Skin<?> |
createDefaultSkin()
Create a new instance of the default skin for this control.
|
private void |
doShow(Node anchor,
double screenX,
double screenY) |
<any> |
getItems()
The menu items on the context menu.
|
<any> |
getOnAction()
Gets the value of the property onAction.
|
void |
hide()
Hides this
ContextMenu and any visible submenus, assuming that when this function
is called that the ContextMenu was showing. |
(package private) boolean |
isShowRelativeToWindow()
*
Private Implementation *
*
|
<any> |
onActionProperty()
Callback function to be informed when an item contained within this
ContextMenu has been activated. |
void |
setOnAction(<any> value)
Sets the value of the property onAction.
|
(package private) void |
setShowRelativeToWindow(boolean value) |
void |
show(Node anchor,
double screenX,
double screenY)
Shows the
ContextMenu at the specified screen coordinates. |
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 hpos and vpos parameters, and offset
by the given dx and dy values for the x-axis and y-axis, respectively. |
getClassCssMetaData, getCssMetaData, getId, getMaxHeight, getMaxWidth, getMinHeight, getMinWidth, getPrefHeight, getPrefWidth, getPseudoClassStates, getSkin, getStyle, getStyleableNode, 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, styleProperty
public final <any> onActionProperty
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.getOnAction()
,
#setOnAction()
private boolean showRelativeToWindow
private <any> onAction
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.private final <any> items
private static final java.lang.String DEFAULT_STYLE_CLASS
public ContextMenu()
public ContextMenu(MenuItem... items)
items
- the list of menu itemspublic final void setOnAction(<any> value)
public final <any> getOnAction()
public final <any> onActionProperty()
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.getOnAction()
,
#setOnAction()
public final <any> getItems()
MenuItem
public void show(Node anchor, Side side, double dx, double dy)
ContextMenu
relative to the given anchor node, on the side
specified by the hpos
and vpos
parameters, 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 hpos
and vpos
parameters,
consider that they are relative to the anchor node. As such, a hpos
and vpos
of CENTER
would mean that the ContextMenu appears
on top of the anchor, with the (0,0) position of the ContextMenu
positioned at (0,0) of the anchor. A hpos
of right would then shift
the ContextMenu
such that its top-left (0,0) position would be attached
to the top-right position of the anchor.
This function is useful for finely tuning the position of a menu, relative to the parent node to ensure close alignment.
anchor
- the anchor nodeside
- the sidedx
- the dx value for the x-axisdy
- the dy value for the y-axispublic void show(Node anchor, double screenX, double screenY)
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 back on screen. This also means that the
ContextMenu
will not span multiple monitors.anchor
- the anchor nodescreenX
- the x position of the anchor in screen coordinatesscreenY
- the y position of the anchor in screen coordinatespublic void hide()
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.
protected Skin<?> createDefaultSkin()
-fx-skin
or set explicitly in a sub-class with setSkin(...)
.createDefaultSkin
in class PopupControl
final boolean isShowRelativeToWindow()
final void setShowRelativeToWindow(boolean value)
private void doShow(Node anchor, double screenX, double screenY)