title: How to close a new popup window from Controller in JavaFX
body: <p>I have Controller.java for Scene.fxml and ControllerSettings.java for WindowSettings.fxml. In Controller.java I create a new popup window (no dialog) with following method:</p>
  
  <pre><code>    @FXML
      public void handleSubmenuSettings(ActionEvent event) throws IOException {
  
          Stage stage; 
          Parent root;
          ControllerSettings controller;
  
          stage = new Stage();
          FXMLLoader loader = new FXMLLoader(getClass().getResource("WindowSettings.fxml"));
          root = (Parent) loader.load();
          controller = (ControllerSettings) loader.getController();
          stage.setScene(new Scene(root));
  
          stage.setTitle("Settings");
          stage.initModality(Modality.APPLICATION_MODAL);
          stage.setResizable(false);
          stage.initOwner(submenuSettings.getScene().getWindow());
          stage.showAndWait();
  
          stage.setOnCloseRequest(e -&gt; {
              e.consume();
              controller.saveSettings();
              stage.close();
          });
      }
  </code></pre>
  
  <p>I want to save the settings when closing the new popup window but that doesn't work with stage.setOnCloseRequest.</p>