首页 > 解决方案 > JavaFX creates Alert dialog which is too small

问题描述

I'm using JavaFX 11.0.1 on Java 11 in Linux (KDE Plasma 5.12.2 on openSUSE Tumbleweed 20190314) to create an Alert dialog which should look like this:

Screenshot showing the JavaFX Alert dialog as expected, large enough to make both the text and the OK button visible.

But most of the time when an Alert is created and shown it is rendered far too small and looks like this:

Screenshot showing the JavaFX Alert dialog so narrow that the only thing visible within it is half of the window's icon.

About one in five times the Alert will display correctly. But the rest of the time the unhelpfully small version is shown instead.

There's nothing unusual about the code being used to display this dialog:

Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Feature absent");
alert.setContentText(
    "Feature \"edit application settings\" has not been finished yet.");
alert.showAndWait();

and I've also tried this advice by adding the following to try to force the size to something useful:

alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.getDialogPane().setMinWidth(Region.USE_PREF_SIZE);

but this gives exactly the same behaviour. (Note that even setting numeric values for the minimum width and height still don't fix the problem.)

Is this a known bug in JavaFX 11? Is there some known workaround which I can use to make sure the Alert dialogs are actually readable?

标签: javajavafxalert

解决方案


已经报告了一个关于完全相同的问题的已知问题,它会影响带有 KDE 的 Linux。可以追溯到这个bug,影响 JavaFX 10 和 11。

该问题提供了一种有效的解决方法:

alert.setResizable(true);
alert.onShownProperty().addListener(e -> { 
    Platform.runLater(() -> alert.setResizable(false)); 
});

但这可能会使警报对话框可调整大小。


推荐阅读