Can we use the creation and use of custom EventHandler classes in JAVAFX?

I have a question about event handling in JavaFX. According to the tutorial (and other examples I've come across), event handling is done in JavaFX as follows:

Button addBtn = new Button("Add"); addBtn.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Add Clicked"); } }); 

But, I wonder if I can “process” the button, click the following path:

 Button addBtn = new Button("Add"); addBtn.setOnAction(new addButtonClicked()); 

where addButtonClicked() is my own class (with its own set of methods and functions), which I defined and wrote to handle actions for clicking a button.

Is there a way to bind custom event handler classes for buttons in JavaFX?

+4
source share
2 answers

EventHandler is an interface class. So it should be “implementation” not “spreading”

 private static class AddButtonClicked implements EventHandler<ActionEvent> { @Override public void handle(ActionEvent event) { System.out.println("My Very Own Private Button Handler"); } } 
+6
source

Sure.

 private static class AddButtonClicked extends EventHandler<ActionEvent> { @Override public void handle(ActionEvent event) { System.out.println("My Very Own Private Button Handler"); } } 
+1
source

Source: https://habr.com/ru/post/1438519/