JavaFx - wrap text content in a dialog box

How to set wraptext parameter in warning dialog? I tried to do this:

    Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
    alert.getButtonTypes().set(0, ButtonType.NO);
    alert.getButtonTypes().set(1, ButtonType.YES);
    alert.getDialogPane().getStylesheets().add("/styles/style.css");
    alert.setGraphic(new ImageView(getIcon(icon)));
    Label lb = (Label) alert.getDialogPane().getChildren().get(1);
    lb.setWrapText(true); //Attempt to set wrapText option
    alert.setTitle(title);
    alert.setHeaderText(header);
    alert.setContentText(content);

But that will not work.

+4
source share
2 answers

Create a new shortcut and set it as the content for the dialog box:

Label label = new Label("Label with\nText that should be wrapped.");
label.setWrapText(true);
alert.getDialogPane().setContent(lb);

Remember that it WrapTextonly wraps end-of-line characters (\ n), and not automatically.

If you want to automatically wrap, use the element Textand set the property WrappingWidth:

Text text = new Text("Very long text that should be wrapped in the dialog");
text.setWrappingWidth(100);
alert.getDialogPane().setContent(text);
+4
source

It's hard for me to print all this, and using Text does not provide any extras, so I use something like this from the lib utility:

public static Alert createAlert(Alert.AlertType type, String message) {
    Alert alert = new Alert(type);

    StringBuilder sb = new StringBuilder(message);
    for (int i = 0; i < message.length(); i += 200) {
        sb.insert(i, "\n");
    }

    Label t = new Label(sb.toString());
    alert.getDialogPane().setContent(t);
    return alert;
}

:

Alert alert = FxUtil.createAlert(Alert.AlertType.ERROR, ex.getMessage());
alert.show();

, - ... :

. : " ...". , ?

0

All Articles