title: Using JavaFX to pass data to an already opened window
body: <p>I'm new to JavaFX. This was easy to do without FXML, but the FXML controllers are stumping me.</p>
  
  <p>What I'm trying to do: Set up a main window that has a button. When clicked, the button launches a second popup window in which the user submits a value.  Upon closing the second window (done currently with a button click on the pop-up), I'd like the user's input to be passed back to the main controller-- the main window that is already open.</p>
  
  <p>So far, I've got 2 .fxml files(one for a main window the other for a popup), and the corresponding controllers: MainWindowController:</p>
  
  <pre><code>public class MainController implements Initializable {
  
  @FXML
  public Label label;
  @FXML
  private Button button;
  
  
  @FXML
  private void popBtnClick(ActionEvent event) throws IOException {
      //creates new pop-up window
      Stage popupSave = new Stage();
      popupSave.initModality(Modality.APPLICATION_MODAL);
      popupSave.initOwner(ComWins.stage);
  
      FXMLLoader loader = new FXMLLoader();
      loader.setLocation(getClass().getResource("PopUp.fxml"));
      Parent root = loader.load();
  
      PopUpController controller = loader.getController();
  
      //calls a method in the PopUpController, and uses it to pass data to 
      //the Popup window.
      controller.dataToPopUp(7);
  
      Scene scene = new Scene(root);
      popupSave.setScene(scene);
      popupSave.showAndWait();
  }
  
  I also tried calling this method from the popup window with no success in 
  changing Main's label.
  public void dataPass(String name){
      label.setText(name);
  }
  
  
  @Override
  public void initialize(URL url, ResourceBundle rb) {
      // TODO
  }    
  
  }
  </code></pre>
  
  <p>And PopUpController:</p>
  
  <pre><code>public class PopUpController implements Initializable {
  
  @FXML
  private Button ok_btn; 
  @FXML
  public TextField input_tf;
  @FXML
  private String input;
  
  
  
  
  @FXML
  private void okBtnClick() throws IOException {
      input = input_tf.getText();
  
      /*my attempt to pass the variable-- using a loader to get the 
       controller and then referencing the public label. */ 
      FXMLLoader loader = new FXMLLoader();
      loader.setLocation(getClass().getResource("Main.fxml"));
      Parent root = loader.load();
  
  
      FXMLDocumentController controller = loader.getController();
  
      //this line works, and retrieves the label's text.
      String temp = controller.label.getText();
  
      //but this line does not work. Why? 
      controller.label.setText(input);
  
  
      //closes this pop-up
      Stage stage = (Stage)input_tf.getScene().getWindow();
      stage.close();
  
  
  }
  
  //this method is called in the maincontroller and used to pass data to 
  //the popup's textfield.
  public void dataToPopUp(int x){
      input_tf.setText(Integer.toString(x));
  }
  
  
   @Override
  public void initialize(URL url, ResourceBundle rb) {
      // TODO
  }    
  
  }
  </code></pre>
  
  <p>Using the above, Main passes ('7') into the PopUp's textfield. But if the user enters something else into the textfield, I cannot seem to get that data back to Main. This is like having a Settings Pop-up window, and then passing the user's selections from the Settings popup back to the main window. I just cannot figure out how to pass things back to the main window.</p>
  
  <p>I am not using SpringBoot, but thanks for the suggestion.</p>
  
  <p>Thank you in advance!</p>