Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Alert and stage focus

3248216Jul 7 2017 — edited Jul 9 2017

Hi, got a question here !
For my application I need to know whenever the app's window is being focused or not. For this, I can use primaryStage.focusedProperty().addListener(..), which will warn me of changes in focus of the stage.

But I have realized that opening an Alert with this primaryStage as owner and with modality set to WINDOW_MODAL makes the `primaryStage` loose focus (even though the window is in reality focused, or at least in Windows).

Now the problem that I have is that I want to know when the Window is focused and not just the primaryStage; or at least know if the Alert is being focused, but I could not find how. I have tried to use similare properties on the alert(like onShowing and onHiding) without success.

Here is a piece of code to illustrate my problem :

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) -> {

                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) -> {

                System.out.println("alert onShowing : "+newValue);

            });

            alert.onShownProperty().addListener((observable, oldValue, newValue) -> {

                System.out.println("alert onShown : "+newValue);

            });

            alert.onHidingProperty().addListener((observable, oldValue, newValue) -> {

                System.out.println("alert onHiding : "+newValue);

            });

            alert.onHiddenProperty().addListener((observable, oldValue, newValue) -> {

                System.out.println("alert onHidden : "+newValue);

            });

            alert.showAndWait();

        }

   

   

        public static void main(String[] args) {

            launch(args);

        }

    }

Basically it will print this :

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'

Which is strange because of all other prints that it should produce.

Also ideally I would need a :

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

Or something similar. Does anyone has an idea to achieve this, or is this primaryStage losing focus to a WINDOW_MODAL Alert an issue that I should report ?

(Repost from StackOverflow, hope it doesn't bother anyone)

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 6 2017
Added on Jul 7 2017
1 comment
2,201 views