I have the following code:
public class Main extends Application {
@FXML
private Button button;
@Override
public void start(Stage primaryStage) {
try {
...
primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent event) {
System.out.println(button)
}
});
...
}
@FXML
private void initialize() {
System.out.println(button);
}
}
I run it, then click on the close button, and this is the output:
Button[id=button, styleClass=button]'button'
null
Why is button null when called inside setOnCloseRequest?