I have a little problem with my JavaFX Application, I want to call a method in a controller from the main Class but it does not work.
I tried this Accessing FXML controller class and this How do I access a UI element from another controller class in JavaFX?
But it does not work.
So, in my Application I have a main window and from there I can open a second window, when I close the second window I want to call a method in the main controller for updating some elements..
My main class have this two Windows:
@Override
public void start(Stage primaryStage) throws IOException {
this.primaryStage = primaryStage;
mainWindow();
public void mainWindow() {
try {
FXMLLoader loader = new FXMLLoader(MainApp.class.getResource("/App.fxml"));
Parent root = loader.load();
AppController appController = loader.getController();
appController.setMain(this);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void secondWindow() throws IOException {
try {
FXMLLoader loader = new FXMLLoader(getClass().getResource("SecondWindow.fxml"));
Parent root = loader.load();
Stage stage = new Stage();
SecondWindowController secondWindowController = loader.getController();
secondWindowController.setStage(stage);
stage.initOwner(primaryStage);
stage.initModality(Modality.WINDOW_MODAL);
stage.setScene(new Scene(root));
stage.show();
stage.setOnCloseRequest(event -> {
event.consume();
Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
alert.setHeaderText("close?");
alert.initOwner( stage);
Optional<ButtonType> result = alert.showAndWait();
if (result.get() == ButtonType.OK){
// Here I want to call the method to update in the AppController
stage.close();
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
Is there a way to call the method there?