Search code examples
javanetbeansjavafx-2keyeventfxml

"Handler method not accessible" error in FXML (works only if I make method with "Event" or "ActionEvent" parameter)


I want to call handler method when any key on keyboard is pressed, and then get pressed key character. So I wrote this line for button in fxml file:

<Button fx:id="button" layoutX="126.0" layoutY="90.0" onKeyPressed="#handleButton" text="Test!" />

When any key is pressed, this should call handleButton method in controller class and pass KeyEvent parameter to it. So I wrote this method inside it:

@FXML
private void handleButton(KeyEvent event) {
    System.out.println(event);
}

In the fxml file NetBeans shows error "Handler method is not accessible. Make public, or annotate with @FXML.", which I've already done.

As soon as I change from private void handleButton(KeyEvent event) to private void handleButton(Event event) NetBeans stops showing error and app works.

On this page I found answer, which uses onKeyPressed exactly the same as I do, so I'm really confused why it isn't working in my case.

Thanks for your help,

Vid


Solution

  • You probably imported wrong KeyEvent. It must be javafx.​scene.​input.KeyEvent.