How to set JavaFX Alert dialog position before it is shown?
Asked Answered
A

3

7

I want to set alert dialog position at right bottom corner after it is shown.

Here is the code:

package alert;

import javafx.application.Application;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Screen;
import javafx.stage.Stage;

public class MainAlert extends Application {

    @Override
    public void start(Stage stage) throws Exception {

        Scene scene = new Scene(createContent());
        stage.setScene(scene);
        stage.setMaximized(true);
        stage.show();
    }

    private Parent createContent() {

        StackPane stackPane = new StackPane();

        Alert alert = new Alert(Alert.AlertType.ERROR);
        alert.setTitle("Error Dialog");
        alert.setHeaderText("Something went wrong");
        alert.setContentText("There is an error!");

        Button alertButton = new Button("Alert test");
        alertButton.setOnAction(event -> {
            Rectangle2D bounds = Screen.getPrimary().getVisualBounds();

            System.out.println("alert.getWidth() = " + alert.getWidth());
            System.out.println("alert.getHeight() = " + alert.getHeight());

            alert.setX(bounds.getMaxX() - alert.getWidth());
            alert.setY(bounds.getMaxY() - alert.getHeight());
            alert.showAndWait();
        });

        stackPane.getChildren().add(alertButton);

        return stackPane;
    }
}

But its position is at left-top corner. The reason is alert.getWidth() and alert.getHeight() are always returning NaN. I already tried Platform.runLater(), unfortunately no use.

How to fix it?

Abirritate answered 20/11, 2017 at 7:59 Comment(0)
T
1

Use hard-coded alert width and height to set alert.setX() and alert.setY() and instantiate Alert inside Action event

private Parent createContent() {

        StackPane stackPane = new StackPane();

        Button alertButton = new Button("Alert test");
        alertButton.setOnAction(event -> {

            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error Dialog");
            alert.setHeaderText("Something went wrong");
            alert.setContentText("There is an error!");
            Rectangle2D bounds = Screen.getPrimary().getVisualBounds();
            alert.setX(bounds.getMaxX() - 366);
            alert.setY(bounds.getMaxY() - 185);

            alert.showAndWait();

        });

        stackPane.getChildren().add(alertButton);

        return stackPane;
    }
Thailand answered 20/11, 2017 at 9:16 Comment(4)
But the position wont' be changed anyway.Abirritate
the Alert Dialog can have different size in different OS or with different Font etc? How to be in this case?Abirritate
and if the actual dialog size will be smaller than position will not work eitherAbirritate
@Abirritate you need to avod use of alert.getWidth() & alert.getHeight()) until oracle comes with new updateThailand
C
1

Try

Alert alert = new Alert(Alert.AlertType.ERROR);
DialogPane pane = alert.getDialogPane();
pane.setPrefHeight(150.0);
alert.setWidth(pane.getWidth());
Rectangle2D bounds = Screen.getPrimary().getBounds();
alert.setX(bounds.getMaxX() - alert.getWidth());
alert.setY(bounds.getMaxY() - pane.getPrefHeight() - 25);

The - 25 is needed if you have a decorated alert window. It's quite ugly but for now the best solution i can come up with. Also you might want to consider the OS' task bar.

Cavefish answered 20/11, 2017 at 10:13 Comment(1)
@Abirritate The -25 is the height of the alert window's frame when you have an Alert with StageStyle.DECORATED (which is the default and can be changed with alert.initStyle(style);). This is not included in the DialogPane's dimensions and therefore needs to be added manually. I'm not sure why an Alert's height and width cant be set properly as it is the case with Stage. If I were you, I'd probably just create my own alert window (using a Stage for example).Cavefish
S
0

If anyone comes across this post, I also was trying to find a way to set the screen location of an Alert dialog. The best way is to use an addEventHandler() with WindowEvent.WINDOW_SHOWN:

alert.getDialogPane().getScene().getWindow().addEventHandler(WindowEvent.WINDOW_SHOWN, event -> setShowPosition());
Stedman answered 11/8 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.