Adding an action listener

Download javafx eBook


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

button.setOnAction(new EventHandler<ActionEvent>() {
    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!"));


Contributors: 2
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial:

Download eBook