xzeo
xzeo

Reputation: 147

JavaFX Convert TextFlow to String

I'm trying to convert a TextFlow to a String in Java.

TextFlow flow = new TextFlow();
Text t1 = new Text(visibility.get(attribute.getVisibility()));
Text t2 = new Text(attribute.getName());
t2.setUnderline(true);
Text t3 = new Text(" : " + attribute.getType());
flow.getChildren().addAll(t1, t2, t3);

this is the way I make my TextFlow, and I'm trying to convert it to a string with all the three Text items next to each other. How should I do this? toString() doesn't work.

Upvotes: 2

Views: 1811

Answers (2)

Jose Martinez
Jose Martinez

Reputation: 11992

This utility method might work.

public static String getStringFromTextFlow(TextFlow tf) {
    StringBuilder sb = new StringBuilder();
    tf.getChildren().stream()
            .filter(t -> Text.class.equals(t.getClass()))
            .forEach(t -> sb.append(((Text) t).getText()));
    return sb.toString();
}

Upvotes: 0

fabian
fabian

Reputation: 82461

TextFlows does not support this. You need to implement this yourself by concatenating all text properties of children of type Text:

StringBuilder sb = new StringBuilder();
for (Node node : flow.getChildren()) {
    if (node instanceof Text) {
        sb.append(((Text) node).getText());
    }
}
String fullText = sb.toString();

Upvotes: 3

Related Questions