6

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?

RSloeserwij
  • 442
  • 3
  • 11
  • What do i need to do to reproduce the behavior when i run your example code? – Markus Köbele Feb 14 '18 at 15:45
  • Don't call `Thread.sleep()` on the JavaFX application thread, all you will do is hang your app. See: [Updating your UI and forcibly waiting before continuing JavaFX](https://stackoverflow.com/questions/35369591/updating-your-ui-and-forcibly-waiting-before-continuing-javafx) – jewelsea Feb 15 '18 at 00:31
  • This is perhaps similar to [How to call launch() more than once in java](https://stackoverflow.com/questions/24320014/how-to-call-launch-more-than-once-in-java) (see the wumpus sample answer). Perhaps you want to call [`setAlwaysOnTop()`](https://docs.oracle.com/javase/9/docs/api/javafx/stage/Stage.html#setAlwaysOnTop-boolean-) like in that sample. – jewelsea Feb 15 '18 at 00:33

1 Answers1

13

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);
DasMoeh
  • 326
  • 2
  • 6