javafx Adding an action listener


Example

Buttons fire action events when they are activated (e.g. clicked, a keybinding for the button is pressed, ...).

button.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {
        System.out.println("Hello World!");
    }
});

If you are using Java 8+, you can use lambdas for action listeners.

button.setOnAction((ActionEvent a) -> System.out.println("Hello, World!"));
// or
button.setOnAction(a -> System.out.println("Hello, World!"));