title: JavaFX onShowing Stage after fxml is loaded
body: <p>what im trying to do is a javaFX window that handles my code <strong>while</strong> my window is shown so i worked on</p>
  
  <pre><code>public void setStage()(Stage stage){
  this.stage = stage;
  stage.setonShowing(new EventHandler&lt;WindowEvent&gt;() {
      @Override
      public void handle(WindowEvent event) {
          work();   //What i want to do while my window is visible
      }
  });
  stage.show();
  }
  </code></pre>
  
  <p>setStage() gets his Stage from the class where i load my fxml from....</p>
  
  <p>Everytime my window is popping up the sleep is <strong>before</strong> the fxml is loaded but <strong>after</strong> the window showed up so i only have a white window with nothing on it</p>
  
  <p>To be honest im not sure what the real problem is</p>
  
  <p>is it that i sleep before the fxml is loaded or is it my setonShowing() which is not working as it should be?
  i also tryed to use <code>setonShown()</code> instead of  <code>setonShowing()</code> but nothing changed</p>
  
  <p>what i also tryed is to override the <code>setonShowing</code> in my Parent class but that also didnt work</p>
  
  <p>so to make sure i didnt made anything wrong with my fxml here is the code i use to creat the window:</p>
  
  <pre><code>        Parent root;
          Stage stage = (Stage) StartSingleplayer_B.getScene().getWindow();
          stage.close(); // closing my old window
  
          try {
  
              fxmlLoader = new FXMLLoader(getClass().getResource("Spielfeld.fxml"));
              root = (Parent) fxmlLoader.load();
              GameController controller = fxmlLoader.getController();
              Stage stage2 = new Stage();
              controller.setStage(stage2);
              stage2.setOnShowing(new EventHandler&lt;WindowEvent&gt;() {
                  @Override
                  public void handle(WindowEvent event) {
                      work();
                  }
              });
              stage2.setScene(new Scene(root));
              stage2.show();
          } catch(Exception e) {
  
              e.printStackTrace();
  
          }   
  </code></pre>
  
  <p>i really appreaciate your help so it would be nice if someone could give me a clue what i did wrong and / or how i could fix it</p>