To sum up the basic requirement: get hold of the node that a contextMenu was opened for. According to the api doc of PopupWindow (the grandparent of ContextMenu), that should be easy to achieve
show(Node node, ...)
... The popup is associated with the specified owner node...
Node getOwnerNode()
The node which is the owner of this popup.
So the general approach in the action of a MenuItem is to
- get hold of the item's parentPopup (that's the contextMenu), might have to work up the ladder if there are nested menus
- grab its ownerNode
- access whatever property is needed
The example at the end does just that in copyText and verifies, that it is working as expected ... iff we are not using a control's contextMenuProperty. The reason for the not-working in controls is a method contract violation (probably introduced by a bug fix around auto-hide behavior in textInputControls) of ContextMenu: it always uses the show(Window w, ..)
after it has been set as contextMenu to any control (implementation detail: Control.contextMenuProperty sets a flag setShowRelativeToWindow(true)
which triggers the mis-behavior)
Now what can we do to get hold of the ownerNode? There are several options, none of which is nice:
- as done in the other answers, somehow keep track of the ownerNode: by using factory method, by storing in the user properties or any other ad-hoc means
- extend ContextMenu, override
show(Node owner, ... )
and keep the given owner in a custom property
- extend ContextMenu, override
show(Node owner, ...)
go dirty and reflectively set super ownerNode to the given
- go dirty and reflectively reset the offending showRelativeToWindow flag back to false after setting the menu to any control
The first two introduce additional coupling, the latter (besides the dirty reflective access) might re-introduce problems with auto-hide (the "fixed" behavior is dirty in itself .. violating the "keep-open-if-owner-clicked" guarantee)
At the end, an example to play with:
public class ContextMenuOwnerSO extends Application {
private Parent createContent() {
TextField text = new TextField();
// the general approach to grab a property from the Node
// that the ContextMenu was opened on
EventHandler<ActionEvent> copyText = e -> {
MenuItem source = (MenuItem) e.getTarget();
ContextMenu popup = source.getParentPopup();
String ownerText = "<not available>";
if (popup != null) {
Node ownerNode = popup.getOwnerNode();
if (ownerNode instanceof Labeled) {
ownerText = ((Label) ownerNode).getText();
} else if (ownerNode instanceof Text) {
ownerText = ((Text) ownerNode).getText();
}
}
text.setText(ownerText);
};
MenuItem printOwner = new MenuItem("copy to text field");
printOwner.setOnAction(copyText);
// verify with manual managing of contextMenu
Text textNode = new Text("I DON'T HAVE a contextMenu property");
Label textNode2 = new Label("I'm NOT USING the contextMenu property");
ContextMenu nodeMenu = new ContextMenu();
nodeMenu.getItems().addAll(printOwner);
EventHandler<ContextMenuEvent> openRequest = e -> {
nodeMenu.show((Node) e.getSource(), Side.BOTTOM, 0, 0);
e.consume();
};
textNode.setOnContextMenuRequested(openRequest);
textNode2.setOnContextMenuRequested(openRequest);
Label label1 = new Label("I'm USING the contextMenu property");
ContextMenu menu = new ContextMenu() {
// force menu to have an owner node: this being the case, it is not hidden
// on mouse events inside its owner
//@Override
//public void show(Node anchor, double screenX, double screenY) {
// ReadOnlyObjectWrapper<Node> owner =
// (ReadOnlyObjectWrapper<Node>)
// FXUtils.invokeGetFieldValue(PopupWindow.class, this, "ownerNode");
// owner.set(anchor);
// super.show(anchor, screenX, screenY);
//}
};
MenuItem item = new MenuItem("copy to text field");
menu.getItems().add(item);
item.setOnAction(copyText);
label1.setContextMenu(menu);
// same effect as forcing the owner node
// has to be done after the last setting of contextMenuProperty
// setting to true was introduced as fix for
// https://bugs.openjdk.java.net/browse/JDK-8114638
//FXUtils.invokeGetMethodValue(ContextMenu.class, menu, "setShowRelativeToWindow", Boolean.TYPE, false);
VBox content = new VBox(10, textNode, textNode2, text, label1);
return content;
}
@Override
public void start(Stage stage) throws Exception {
stage.setScene(new Scene(createContent(), 400, 200));
stage.setTitle(FXUtils.version());
stage.show();
}
public static void main(String[] args) {
launch(args);
}
@SuppressWarnings("unused")
private static final Logger LOG = Logger
.getLogger(ContextMenuOwnerSO.class.getName());
}