title: How to close all stages when the primary stage is closed?
body: <p>I have an application where I call the following code when an exception occurs- that is, it is located within the "catch" of a "try/catch". It merely creates a "popup"-like window which tells the user to enter an int instead.</p>
  
  <pre><code>Stage dialogStage = new Stage();
  dialogStage.initModality(Modality.WINDOW_MODAL);
  dialogStage.setScene(new Scene(VBoxBuilder.create().
  children(new Text("Please enter an integer."), new Button("Ok.")).
  alignment(Pos.CENTER).padding(new Insets(5)).build()));
  dialogStage.show();
  </code></pre>
  
  <p>The only problem is that the stage lingers even if the primary stage (that is the parameter in the public void start(Stage primaryStage)) is closed.</p>
  
  <p>What have I tried?:</p>
  
  <p>I made the dialogstage visible in the whole class by defining it as a class variable. Then I implemented the following code:</p>
  
  <pre><code>primaryStage.setOnCloseRequest((new EventHandler&lt;WindowEvent&gt;(){
  
          @Override
          public void handle(WindowEvent arg0) {
                              arg0.consume();
              try
              {
               dialogStage.close();
  
                              }
              catch(Exception ex)
              {
                  System.out.print(ex.getMessage()+"\r\n");
              }
  
          }
      }));
  }
  </code></pre>
  
  <p>This "works" in that the dialogstage is closed when a user tries to exit the main application, however, it does not actually do what I intended: when you try to close the primary stage only the dialogstage is closed.</p>
  
  <p>It is also an aesthetically displeasing solution as I don't want the whole class to know about the stupid dialog box which is only located in one try/catch and might never be used.</p>
  
  <p>I'd like to know if there is a simple solution that tells the dialogstage to close when the primary stage does and where I don't have to write much code outside of my try/catch. </p>