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?

Answer

There is a known issue already reported on the exact same problem, affecting Linux with KDE. It can be traced back to this bug, affecting JavaFX 10 and 11.

The issue provides a workaround that works:

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

but that might leave the alert dialog resizable.

Leave a Reply

Your email address will not be published. Required fields are marked *