11

removeEventHandler() is ok, but what if I don't keep reference on handler ?

Can I remove any event handler(filter) by event type or even all handlers from my JavaFX. scene.Node instance? I guess that somewhere a list of handlers existed, and I can traverse it, and remove what I want.

ARno
  • 314
  • 3
  • 14
Andrew
  • 592
  • 1
  • 5
  • 17

2 Answers2

5

Can I remove any event handler(filter) by event type or even all handlers from my javafx.scene.Node instance?

I don't think you can remove an event handler or filter which you didn't have a reference to originally. You can add extra event filters to filter out processing for events by type or you can set your own event dispatcher on the node and have your custom dispatcher only forward the events you want to the node's standard event dispatcher.

I guess that somewhere a list of handlers existed, and I can traverse it, and remove what I want.

Yes, but that is buried within the private implementation of the Node, so you probably don't want to hack the private Node code to do that.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
4

I came across this question while looking for how to create event handlers that remove themselves. The answer to my question was here, I don't know if it will help you. javafx have an eventfilter remove itself

Here is an example

EventHandler<MouseEvent> object_clicked=new EventHandler<MouseEvent>() {
    @Override
    public void handle(MouseEvent event) {
        // on click actions here

        my_node.removeEventFilter(MouseEvent.MOUSE_CLICKED, this); // at the bottom
    }
};

my_node.addEventFilter(MouseEvent.MOUSE_CLICKED, object_clicked); // add the eventhandler to the node
Community
  • 1
  • 1
Joseph Sang
  • 310
  • 2
  • 9