user2414483
user2414483

Reputation: 60

"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

Upvotes: 4

Views: 5111

Answers (2)

Soufyan Guenbour
Soufyan Guenbour

Reputation: 11

Right-click with the mouse on your file.FXML and choose from options make controller and automatically the problem will be resolved and this error will be gone.

Upvotes: 1

Uluk Biy
Uluk Biy

Reputation: 49185

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

Upvotes: 13

Related Questions