JavaFX – setOnAction not applicable

You have imported awt event listener just change this line of code

import java.awt.event.ActionEvent;

with this

import javafx.event.ActionEvent;

and you can also use lambda expression like this

btn.setOnAction((event) -> {
  System.out.println("Button clicked");
});

Leave a Comment