Adam Ren Adam Ren - 2 months ago 7
Java Question

Program Flashes Upon Opening of Popup Window

Created a full screen application with a popup window opening up upon pressing the ESCAPE key. Once run however the application flashes and then shows the popup window.

//Pop-up window
Stage window = new Stage();
window.initModality(Modality.NONE);
//Exit Panel
VBox exitBox = new VBox();
exitBox.setPadding(new Insets(10));
Button exitPaneExit = new Button();
exitPaneExit.setText("Return");
exitPaneExit.setMinSize(75.0, 30.0);
exitPaneExit.setOnAction(e -> {
window.close();
});
Button exitButton = new Button();
exitButton.setText("Exit");
exitButton.setMinSize(75.0, 30.0);
exitButton.setOnAction(e -> {
System.exit(0);
});
exitBox.getChildren().addAll(exitPaneExit,exitButton);
exitBox.setVisible(true);
Scene scene = new Scene(exitBox);
window.initStyle(StageStyle.UNDECORATED);
window.initOwner(primaryStage);
window.setScene(scene);
mapScene.setOnKeyPressed(e -> {
if(e.getCode()==KeyCode.ESCAPE)
{
window.show();
}
});


The code runs fine and I get no errors however the application flashes upon opening the popup which is QUITE annoying.

Answer

I finally found the solution myself through the implementation of the Popup class. This is effect is the same as the creation of a transparent/utility stage with an initOwner, but is built into JavaFX. When shown it requires an initOwner so that is the argument I placed there.

Here is the code:

The code relevant to the actual popup.

    private Popup addExitPopup(){
    Popup exitPopup = new Popup();
    //Exit Panel
    VBox exitBox = new VBox();
    exitBox.setPadding(new Insets(10));
    Button exitPaneExit = new Button();
    exitPaneExit.setText("Return");
    exitPaneExit.setMinSize(75.0, 30.0);
    exitPaneExit.setOnAction(e -> {
        exitPopup.hide();
    });
    Button exitButton = new Button();
    exitButton.setText("Exit");
    exitButton.setMinSize(75.0, 30.0);
    exitButton.setOnAction(e -> {
        System.exit(0);
    });
    exitBox.getChildren().addAll(exitPaneExit,exitButton);
    exitBox.setVisible(true);

    exitPopup.setAutoHide(true);
    exitPopup.getContent().add(exitBox);
    return exitPopup;
}

The code relevant to making it appear in a full screen application.

    @Override
public void start(Stage primaryStage) throws Exception {
    Popup exitPopupO = addExitPopup();
    AnchorPane mapAnchorO = addMapAnchor();
    Scene mapScene = new Scene(mapAnchorO);
    primaryStage.setScene(mapScene);
    primaryStage.setFullScreen(true);
    primaryStage.setResizable(false);
    primaryStage.setFullScreenExitKeyCombination(KeyCombination.NO_MATCH);
    primaryStage.show();

    mapScene.setOnKeyPressed(e -> {
        if(e.getCode()==KeyCode.ESCAPE)
        {
            exitPopupO.show(primaryStage);
        }
    });
}