A method handler error is not available in FXML (it only works if I create a method with the parameter "Event" or "ActionEvent")

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

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

When any key is pressed, this should call the handleButton method in the controller class and pass the 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 the error "The handler method is not available. Publishing or annotation using @FXML.", Which I already made.

As soon as I switch from private void handleButton(KeyEvent event) to private void handleButton(Event event) , NetBeans stops showing errors and the application runs.

On this page, I found an answer that uses onKeyPressed exactly the same as me, so I'm really confused why this is not working in my case.

Thanks for your help,

Vid

+4
source share
1 answer

You probably imported the wrong KeyEvent . It should be javafx.โ€‹scene.โ€‹input.KeyEvent .

+9
source

Source: https://habr.com/ru/post/1493909/


All Articles