In javaFX need object reference to node under mouse cursor


Question

I need a reference to the object under the mouse cursor in a javaFX Pane (or ScrollPane) The following code removes the node under the cursor from the Pane.

The node object is stored in an ArrayList and has a unique id. If I had a reference to the node object, it would be a simple matter to remove it from the list. I'd be happy just to be able to pull the id of the node object out of the target description supplied by the MouseEvent's description of the target, and use it to find my node in the list. Note: I am creating these nodes dynamically and they are nameless: myList.add(new TYPE()).

Here's the snippet that I'm using to remove the node in the Pane;

root.setOnMouseClicked(new EventHandler<MouseEvent>() {
    @Override
    public void handle (MouseEvent me) {
        // would like a line here that grabbed the reference to the target
        pane.getChildren().remove(me.getTarget());

    }
});
1
1
10/2/2014 4:31:29 AM

It would be better for you to put the setOnMouseClicked listener to the node on which you want to remove from the list.

List<Node> listOfNodes = new ArrayList();
Node node = new Node();
listOfNodes.add(node);
node.setOnMouseClicked((event) -> {
    listOfNodes.remove(node);
});

Note: Better use ObservableList than the ordinary ArrayList especially if you want that node to be removed also out from the root pane.

1
10/2/2014 5:18:00 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon