JavaFX Alerts and their size
Another solution is subclassing the Alert and applying desired style there, for example:
class SubAlert extends Alert {
{
setHeaderText("");
getDialogPane().getStylesheets().add("some_stylesheet");
getDialogPane().getStyleClass().add("style_class");
getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
}
SubAlert(AlertType alertType) {
super(alertType);
}
SubAlert(AlertType type, String title, String content) {
super(type);
setTitle(title);
setContentText(content);
}
}
This way you don't have to repeat actions for every Alert you create.
I have made the following workaround:
Alert alert = new Alert(AlertType.INFORMATION, "Content here", ButtonType.OK);
alert.getDialogPane().setMinHeight(Region.USE_PREF_SIZE);
alert.show();
So the window will resize automatically according to the content.
Here is the better workaround without magic numbers, resizing etc.:
Alert alert = new Alert(AlertType.ERROR, "content text");
alert.getDialogPane().getChildren().stream().filter(node -> node instanceof Label).forEach(node -> ((Label)node).setMinHeight(Region.USE_PREF_SIZE));
This solution works under Windows, Linux and Mac.
I have made the following workaround sometime ago:
Alert dialog = new Alert(Alert.AlertType.ERROR);
dialog.setHeaderText("Connection Failed");
dialog.setContentText(this.getException().getMessage());
//FIXME: Remove after release 8u40
dialog.setResizable(true);
dialog.getDialogPane().setPrefSize(480, 320);
dialog.showAndWait();
As you can see I just set resizable flag and set preferred size.
But this is strange because this bug should be fixed in 8u40. Are you using latest build of 8u40?
UPDATE:
Not fixed in 8u40. Should be fixed later.