简体   繁体   中英

How does JavaFx's setOnAction method work under the hood?

In my object oriented programming class, our recent topic has been event-driven programming through JavaFX. The concepts have been relatively interesting, though at times quite confusing to me. One thing that I have struggled quite greatly to understand is the workings of the setOnAction() method.

As my instructor said, this method takes in an object of a class that has a handle method, and calls that handle method whenever an event is fired.

For example in our start method:

class MyHandler implements EventHandler<ActionEvent> {
        @Override
        public void handle(ActionEvent actionEvent) {
            System.out.println("Handled");
        }
    }

Button button = new Button();
button.setOnAction(new MyHandler());

However, I'm unable to understand how this works under the hood. How exactly does the setOnAction method "know" to call the handle method of the MyHandler object? Is it possible to implement my own version of a setOnAction , in which I call a specifically named method of any passed in object?

And what if we don't implement a handle method at all, but instead say, a fakeHandle method, like this.

class MyHandler {
        public void fakeHandle(ActionEvent actionEvent) {
            System.out.println("Handled");
        }
    }

Button button = new Button();
button.setOnAction(new MyHandler());

Would our code still work?

As my instructor said, this method takes in an object of a class that has a handle method, and calls that handle method whenever an event is fired.

I suspect there's a slight misunderstanding or a poor choice of words happening here. You cannot pass any arbitrary object whose class defines a handle(ActionEvent) method to the setOnAction method. The setOnAction method is defined to accept an EventHandler<ActionEvent> implementation, and it's that interface that defines the handle method.

This is exactly what interfaces are for. They define a contract that all implementations must follow. It doesn't matter what object you pass to setOnAction so long as its class implements EventHandler<ActionEvent> . And that's how JavaFX knows about the handle method, because it knows about the EventHandler interface.

Your second code example won't compile because myHandler doesn't implement EventHandler<ActionEvent> . But even if it did your fakeHandle method would not be invoked (by JavaFX code) because JavaFX is not aware of it.


Explaining exactly how the entire event-handling process works under-the-hood in JavaFX is an in-depth process. If you really want to understand this then I suggest reading the code— JavaFX is open source . However, if you only want to understand event-handling from a user's perspective (ie an API level) then this tutorial should help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM