After following the tutorial at http://www.javafxtutorials.com/tutorials/switching-to-different-screens-in-javafx-and-fxml/, I have a functioning popup window, however, I am having difficulty figuring out how to interact with it with a controller.
From the tutorial, I got the impression that there is to be a shared controller between the two FXML files, but I'm having trouble referencing the new stage.
To this, I have a few questions.
@FXML
private void toOutput(ActionEvent event) throws Exception {
Stage stage;
Parent root;
stage = new Stage();
root = FXMLLoader.load(getClass().getResource("TextWindow.fxml"));
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(searchBox.getScene().getWindow());
stage.showAndWait();
}
1) When the above is run, does the stage.showAndWait() create a new scene instance with a new controller, or does it work off of the existing scene controller?
2a) If it uses a new controller, is this beneficial? I'm guessing the controller goes through some other wrapping to build things, so it may only pull what it needs off of the @FXML tags, but I have local variables and etc that seems like it's not a good idea
2b) If it uses the existing controller, how do I reference the variables local to the class instance? It's probably extremely simple, but FXML in general is still quite alien to me.
3) If I were to use a completely separate controller for (presuming I cannot get it to work with a single controller), how would I pass a data element from one controller to another? I see that the initialize() method has an argument for a ResourceBundle, but I'm not certain how to utilize this.
Answer Edit
Correct Code:
@FXML
private void toOutput(ActionEvent event) throws Exception {
Stage stage = new Stage();
Parent root;
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("OutputWindow.fxml"));
root = loader.load();
OutputWindowController controller = loader.getController();
controller.setMat(mat);
stage.setScene(new Scene(root));
stage.initModality(Modality.APPLICATION_MODAL);
stage.initOwner(searchBox.getScene().getWindow());
stage.showAndWait();
}