title: javafx - Alert and stage focus
body: <p>For my application I need to know whenever the app's window is being focused or not. For this, I can use <code>primaryStage.focusedProperty().addListener(..)</code>, which will warn me of changes in focus of the stage.</p>
  
  <p>But I have realized that opening an <code>Alert</code> with this <code>primaryStage</code> as owner and with modality set to <code>WINDOW_MODAL</code> makes the <code>primaryStage</code> loose focus (even though the window is in reality focused, or at least in Windows). </p>
  
  <p>Now the problem that I have is that I want to know when the <code>Window</code> is focused and not just the <code>primaryStage</code>; or at least know if the <code>Alert</code> is being focused, but I could not find how.</p>
  
  <p>I have tried to use similare properties on the alert(like <code>onShowing</code> and <code>onHiding</code>) without success.</p>
  
  <p>Here is a piece of code to illustrate my problem : </p>
  
  <pre><code>package sample;
  
  import javafx.application.Application;
  import javafx.fxml.FXMLLoader;
  import javafx.scene.Parent;
  import javafx.scene.Scene;
  import javafx.scene.control.Alert;
  import javafx.stage.Modality;
  import javafx.stage.Stage;
  
  public class Main extends Application {
  
      @Override
      public void start(Stage primaryStage) throws Exception{
          Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
          primaryStage.setTitle("Hello World");
          primaryStage.setScene(new Scene(root, 300, 275));
  
          primaryStage.focusedProperty().addListener((observable, oldValue, newValue) -&gt; {
              System.out.println("primaryStage focused : "+newValue);
          });
          primaryStage.show();
  
          //create a basic alert
          Alert alert = new Alert(Alert.AlertType.INFORMATION,"This is a test");
          alert.initModality(Modality.WINDOW_MODAL); //will block input to its owner window
          alert.initOwner(primaryStage);
          alert.onShowingProperty().addListener((observable, oldValue, newValue) -&gt; {
              System.out.println("alert onShowing : "+newValue);
          });
          alert.onShownProperty().addListener((observable, oldValue, newValue) -&gt; {
              System.out.println("alert onShown : "+newValue);
          });
          alert.onHidingProperty().addListener((observable, oldValue, newValue) -&gt; {
              System.out.println("alert onHiding : "+newValue);
          });
          alert.onHiddenProperty().addListener((observable, oldValue, newValue) -&gt; {
              System.out.println("alert onHidden : "+newValue);
          });
          alert.showAndWait();
      }
  
  
      public static void main(String[] args) {
          launch(args);
      }
  }
  </code></pre>
  
  <p>Basically it will print this :</p>
  
  <pre><code>primaryStage focused : true //stage is created and displayed
  primaryStage focused : false //alert is displayed, stage loses focus. alt+tab changes nothing
  primaryStage focused : true //alert closed by pressing 'ok'
  </code></pre>
  
  <p>Which is strange because of all other prints that it should produce.
  Also ideally I would need a :</p>
  
  <pre><code>primaryStage focused : true //stage is created and displayed
  primaryStage focused : false //alert is displayed, stage loses focus
  alert focused : true //alert gains focus
  alert focused : false //alt+tab to an other window
  alert focused : true //alt+tab back to this window
  alert focused : false //alert closed by pressing 'ok'
  primaryStage focused : true //stage regains focus
  </code></pre>
  
  <p>Or something similar. Does anyone has an idea to achieve this, or is this <code>primaryStage</code> losing focus to a <code>WINDOW_MODAL</code> <code>Alert</code> an issue that I should report ?</p>