Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bringing a single stage to the front in windows in JavaFX

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?

like image 871
RSloeserwij Avatar asked Feb 14 '18 09:02

RSloeserwij


1 Answers

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);
like image 75
DasMoeh Avatar answered Sep 24 '22 15:09

DasMoeh