I have a "parent" window with a ListView and a button. The idea is that clicking the button opens a new "child" window (the "parent" window stays in the background) with an "accept" button.
In the "child" window I fill an ArrayList and by clicking "ok" I close the "child" window and send the ArrayList to the "parent" window.
I have to repeat this many times, until I manage to populate several collections in the "parent" window using several "child" windows, so I cannot afford to create a new "parent" window each time.
This is the code I use to open the "child" window:
private void anadirMateriales(){
abrirVentana("AnadirMateriales.fxml", "Añadir materiales");
}
public ArrayList<String> materiales = new ArrayList<>();
private void abrirVentana(String fxml, String titulo){
try {
FXMLLoader fxmlLoader = new FXMLLoader();
fxmlLoader.setLocation(getClass().getResource(fxml));
Scene scene = new Scene(fxmlLoader.load());
Stage stage = new Stage();
stage.setTitle(titulo);
stage.setScene(scene);
stage.initModality(Modality.APPLICATION_MODAL);
stage.showAndWait();
} catch (IOException ex) {
System.out.println("IO Exception: " + ex.getMessage());
}
}
And in the "child" window the only thing I have is the necessary code to fill an ArrayList called "materials".
Thanks in advance for the help, I don't know what I would do without this community
My recommendation is that you implement a method in the controller of the child window which receives an object of type list as a parameter (eg
List
). Using this method you can pass the instance of the list you want to fill into the child window (in your casemateriales
) and when it is closed the list will contain the data you need.Then, before displaying the child view, you must get the reference to the controller and call the created method in the controller by passing the list instance (
materiales
) to it.Despite having marked E.Betanzos' answer as an answer because it helped me with the problem I was trying to solve, which I am also very grateful for, I have come across a similar case in which, for some reason that I still don't know, I couldn't return the "parent" window the data that it had sent me and then manipulated in the "child" window.
The solution has been the following:
I hope this will help someone with the same problem as me in the future.
All the best!