Shersha Fn
Shersha Fn

Reputation: 1571

Javafx fullscreen application gets minimized when alert message is shown

I am making a javafx application which is in full screen mode.My application gets minimized when I try to show an alert message

Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(null);
alert.setHeaderText(null);
alert.setContentText("I have a great message for you!");
alert.show();

and when I click ok button on alert Box application switches back to full screen mode.How to show alert box message with in full screen without minimizing application .

Upvotes: 0

Views: 2895

Answers (2)

Rupert
Rupert

Reputation: 91

I'm not really sure why showing the Alert (i.e. Dialog) ends fullscreen mode. I think it's because the dialog itself is a native window that gains focus. From the JavaFX API docs (class Stage):

The full-screen mode will be exited (and the fullScreen attribute will be set to false) if the full-screen Stage loses focus or if another Stage enters full-screen mode on the same Screen. Note that a Stage in full-screen mode can become invisible without losing its full-screen status and will again enter full-screen mode when the Stage becomes visible.

Given that as a plausible reason I tried to exchange the Scene of the Stage getting the same result. Probably exchanging the Scene has an impact on recalculating the Stage (i.e. window) size. Next try was to exchange the root of the Scene and this seems to work. The drawback is that you lose the comfortable return values of the Alert.

public class FullScreenAlert extends Application {
    @Override
    public void start(Stage primaryStage) throws Exception {
        Pane rootPane = new StackPane();
        Scene mainScene = new Scene(rootPane);
        Button btn = new Button("alert");
        rootPane.getChildren().add(btn);

        Rectangle blockingRect = new Rectangle();
        blockingRect.widthProperty().bind(primaryStage.widthProperty());
        blockingRect.heightProperty().bind(primaryStage.heightProperty());
        blockingRect.setFill(Color.LIGHTBLUE);
        blockingRect.setOpacity(0.5);

        VBox alertPane = new VBox(10);
        alertPane.setBackground(new Background(new BackgroundFill(Color.GRAY, CornerRadii.EMPTY, Insets.EMPTY)));
        alertPane.setMaxWidth(400);
        alertPane.setMaxHeight(200);
        alertPane.setAlignment(Pos.CENTER);
        Label alertMessage = new Label("alert message");
        Button alertOKButton = new Button("OK");
        alertPane.getChildren().addAll(alertMessage, alertOKButton);

        btn.setOnAction(event -> {
            rootPane.getChildren().addAll(blockingRect, alertPane);
        });
        alertOKButton.setOnAction(event -> {
            rootPane.getChildren().removeAll(blockingRect, alertPane);
        });

        primaryStage.setFullScreen(true);
        primaryStage.setScene(mainScene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

So the conclusion is that you can modify the current Scene content without touching the window size and therefore without losing fullscreen mode. This way it should be possible to also paint a pretty pseudo-dialog instead of that one above.

I know this certainly is not the best solution because there will only be one Scene for the whole Application. But I'm not aware of a better way to keep the fullscreen mode.

Upvotes: 1

jewelsea
jewelsea

Reputation: 159566

Initialize the owner window for the alert to your full screen stage.

alert.initOwner(primaryStage)

Upvotes: 10

Related Questions