I have an fxml that describes my gui. I want to change text of the gui and start a task on any key press anywhere.
FXML
<Text fx:id="barcodeText"/>
Controller
@FXML
Text barcodeText;
public void start(Stage primaryStage) throws IOException {
this.primaryStage=primaryStage;
Scene mainScene =new Scene(root);
primaryStage.setScene(mainScene);
primaryStage.setResizable(false);
primaryStage.show();
Parent root = FXMLLoader.load(getClass().getResource("/view/foo.fxml"));
mainScene.addEventHandler(KeyEvent.KEY_PRESSED,new KeyboardEventHandler(){
@Override
public void handle(KeyEvent event) {
barcodeText.setText("foo");
}
});
This gives me a NullPointerException
(inside JavaFX Application Thread) for the barcodeText pointer when I fire the event.
Am I doing something wrong?
The examples I looked at were using this approach without fxml, do I have to use an annotation to define the handler? where would I put "onAction" for the scene in the fxml?