1

I am new in JavaFX and fail in what I need to do: I change scene this way and at the same time define an event filter:

    AnchorPane pane = FXMLLoader.load(getClass().getResource("blabla.fxml"));
    Scene scene = new Scene(pane);

    scene.addEventFilter(KeyEvent.KEY_PRESSED, new EventHandler<KeyEvent>() {
                        public void handle(KeyEvent event) {
                            System.out.println("Filtering event " + event.getEventType() + ", " + event.getCode());

        //Here in the handle method I want to call a method of the current controller

                        }
                    });

     Stage appStage = (Stage) ((Node) event.getSource()).getScene().getWindow();
     appStage.setScene(scene);

 appStage.show();

In the handle method of the event filter I want to execute a particular method of the loaded controller (change images). I cannot put a setOnKeyPressed in this controller since the associated fxml has only images key pressed are not catched. I read that I had to use the addEventFilter on the scene. It works. The handle method is correctly triggered but I do not know how to access a method on the controller associated to "blabla.fxml".

Many many thanks for your help.

Dominique

  • 2
    Possible duplicate of [Accessing FXML controller class](https://stackoverflow.com/questions/10751271/accessing-fxml-controller-class) – Slaw Mar 21 '19 at 20:48
  • Might also help: https://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – Slaw Mar 21 '19 at 20:49

0 Answers0