Adding an EventHandler to a JavaFX Button
Adding EventHandler to JavaFX Button
In the JavaFX GUI toolkit, the Button control provides a "fire" event when the user clicks the button. To capture this event, you'll need to create an EventHandler and attach it to the button.
The EventHandler is an interface, so to use it you must create a class that implements the interface. Here's a class that implements the EventHandler interface:
public class MyEventHandler implements EventHandler<ActionEvent> {
public void handle(ActionEvent event) {
// do something here
}
}
Once your class implements the interface, you can create an instance of the class and attach it to the button.
Button myButton = new Button();
MyEventHandler handler = new MyEventHandler();
myButton.setOnAction(handler);
Every time the user clicks the button, the handle
method of your MyEventHandler
class will be invoked.