-->

JavaFX ContextMenu how do I get the clicked Object

2019-05-11 08:31发布

问题:

I am learning javafx.scene.control.ContextMenu, and right now I am facing a problem:

how do I get the clicked Object from EventHandler? both event.source() and event.target() return the MenuItem.

let me explain with an example: what should I write inside the function handle?

    TextField text = new TextField();
    Label label1 = new Label("hello");
    Label label2 = new Label("world");
    Label label3 = new Label("java");

    ContextMenu menu = new ContextMenu();
    MenuItem item = new MenuItem("copy to text field");
    menu.getItems().add(item);
    item.setOnAction(new EventHandler(){
        public void handle(Event event) {
            //I want to copy the text of the Label I clicked to TextField
            event.consume();
        }
    });

    label1.setContextMenu(menu);
    label2.setContextMenu(menu);
    label3.setContextMenu(menu);

EDIT: I was hoping there was some simple solution (one liner), but if there isn't then there are lot's of complex way to do it.

回答1:

You could create your own instance of ContextMenu and add the action parent to it for further reference:

public class Main extends Application {

    TextField text = new TextField();

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {


        Label label1 = new Label("hello");
        Label label2 = new Label("world");
        Label label3 = new Label("java");

        label1.setContextMenu(new MyContextMenu(label1));
        label2.setContextMenu(new MyContextMenu(label2));
        label3.setContextMenu(new MyContextMenu(label3));

        HBox root = new HBox();

        root.getChildren().addAll(text, label1, label2, label3);

        Scene scene = new Scene(root, 300, 100);

        primaryStage.setScene(scene);
        primaryStage.show();

    }

    private class MyContextMenu extends ContextMenu {

        public MyContextMenu(Label label) {

            MenuItem item = new MenuItem("copy to text field");
            item.setOnAction(event -> {

                // I want to copy the text of the Label I clicked to TextField
                text.setText(label.getText());

                event.consume();
            });

            getItems().add(item);

        }

    }
}


回答2:

Just create a different ContextMenu instance for each label:

TextField text = new TextField();
Label label1 = new Label("hello");
Label label2 = new Label("world");
Label label3 = new Label("java");

label1.setContextMenu(createContextMenu(label1, text));       
label2.setContextMenu(createContextMenu(label2, text));            
label3.setContextMenu(createContextMenu(label3, text));

// ...

private ContextMenu createContextMenu(Label label, TextField text) {
    ContextMenu menu = new ContextMenu();
    MenuItem item = new MenuItem("copy to text field");
    menu.getItems().add(item);
    item.setOnAction(new EventHandler(){
        public void handle(Event event) {
            text.setText(label.getText());
        }
    });
    return menu ;
}


回答3:

I think the easiest way is to save the Node as UserData of context menu.

EventHandler<? super ContextMenuEvent> eventHandle = e->menu.setUseData(e.getSource());
label1.setOnContextMenuRequested(eventHandle );
label2.setOnContextMenuRequested(eventHandle );
label3.setOnContextMenuRequested(eventHandle );

and in action:

EventHandler<ActionEvent> menuItemEvent = e->{
    Node node = (Node) ((MenuItem)e.getSource()).getParentPopup().getUserData();
   ...
};


回答4:

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());

}