JavaFX Alerts and their size

2019-01-14 10:51发布

问题:

Recently, JavaFX introduced Alerts (Java 8u40).

Consider the code example below. How can I display a full message that is longer than just a few words? My messages (contentText property) get cut at the end with ... and the Alert does not adjust its size properly in my opinion.

On my Linux machine with Oracle JDK 8u40, I only see the text This is a long text. Lorem ipsum dolor sit amet, which is too short in some cases.

Of course, the user can resize the Alert window manually and the text will be displayed accordingly, but that is not user-friendly at all.

Edit: Screenshots for Windows 7 and Linux (JDK from Oracle):

import javafx.application.Application;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.stage.Stage;


public class TestAlert extends Application {
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        Alert a = new Alert(AlertType.INFORMATION);
        a.setTitle("My Title");
        a.setHeaderText("My Header Text");
        a.setResizable(true);
        String version = System.getProperty("java.version");
        String content = String.format("Java: %s.\nThis is a long text. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.", version);
        a.setContentText(content);
        a.showAndWait();
    }
}

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.