7

I am trying to create a stage in JavaFX that can pop up to the front in windows, while the main stage stays minimized.

This only works however, when the main stage is visible on the screen. I have tried making it work using Modality, but then the user can't interact with the main stage, which is not what i want.

The problem can be reproduced with the following Application:

public class MainApp extends Application {

@Override
public void start(Stage stage) throws Exception {
    Scene mainScene = new Scene(new Parent() {});
    Stage mainStage = new Stage();
    mainStage.setScene(mainScene);
    mainStage.show();
    mainStage.setIconified(true);

    Scene popUpScene = new Scene(new Parent() {});
    Stage popUpStage = new Stage();
    popUpStage.setScene(popUpScene);

    Thread.sleep(5000);
    popUp(popUpStage);
}

public static void popUp(Stage popUpStage){
    if (popUpStage.isIconified()) popUpStage.setIconified(false);
    popUpStage.show();
    popUpStage.requestFocus();
    popUpStage.toFront();       
}

}

Is there anyone who has an answer to this problem?

3

1 Answer 1

14

Just add these two line to popUp. First line brings it to front. Second line allows interaction with the main stage or other windows.

popUpStage.setAlwaysOnTop(true);
popUpStage.setAlwaysOnTop(false);
Sign up to request clarification or add additional context in comments.

Thanks this solved my problem, all you have to do apart from that is making sure your window is not iconified.
It also solved the problem for me. Thanks for that solution. Nevertheless, I ask myself, why Stage.toFront() does not work in my case...
It's a little "hackish"

Your Answer

Draft saved
Draft discarded

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.