1

The following code dynamically creates a TextFlow and Text that originally has new line characters in it (potentially images and other panes).

InputStream stream = new ByteArrayInputStream(text.toString().getBytes(StandardCharsets.UTF_8));
      TextFlow element = new FXMLLoader().load(stream);

However, the ByteArrayInputStream method with FXML strips out all the new lines.

Tried using both Windows&Unix end of line characters, not that that should make a difference anyway, but you get to that point when you try different combinations in vain.

Usually it's recommended to use a BufferedReader to get the new lines back, but here I'm not in control. The other load methods seem to take URL's and resource links, however this is a manipulated string.

An example

The java code:

package simpleexample;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.TextFlow;
import javafx.stage.Stage;

import com.jcabi.xml.XMLDocument;

public class TestFxmlWithBreaks extends Application {


  @Override
  public void start(Stage primaryStage) {
    Scene scene = new Scene(createEntry(), 750, 750, Color.web("#666970"));
    primaryStage.setScene(scene);
    primaryStage.show();

  }


  private VBox createEntry() {
    VBox entryBox = new VBox();
    entryBox.setFillWidth(true);

    entryBox.setSpacing(20);
    entryBox.setMaxWidth(750);

    try {
      XMLDocument test = new XMLDocument(this.getClass().getResourceAsStream("inputFxml.xml"));
      System.out.println(test.toString());

      InputStream stream = new ByteArrayInputStream(test.toString().getBytes(StandardCharsets.UTF_8));
      TextFlow element = new FXMLLoader().load(stream);
      element.setMaxWidth(750);

      entryBox.getChildren().add(element);
    } catch (Exception e) {
      //log something
    }

    return entryBox;
  }


  public static void main(String[] args) {
  launch(args);
  }



}

inputFxml.xml:

<?xml version="1.0" encoding="UTF-8"?><?import javafx.scene.text.*?><TextFlow xmlns:fx="http://javafx.com/fxml" styleClass="paragraph">
<Text style="-fx-font-size: 20;">A list of stuff</Text>
<Text>
* stuff
* more stuff
* even more stuff, all with new lines
</Text>
<Text style="-fx-font-size: 15;">This should demonstrate it</Text>
</TextFlow>
Community
  • 1
  • 1
Daniel Gerson
  • 2,159
  • 1
  • 19
  • 29
  • Can you show an example of `text.toString()` that fails here? I.e. make a [MCVE] somehow? It's kind of hard to see what you are trying to do... why not just create the `TextFlow` using Java, instead of FXML, and generate the text content? – James_D Dec 06 '15 at 20:43
  • No problem sir. I've added a simple example. The XMLDocument here is essentially a no-op, but in reality there would be XSL transformations. I can't create it using java, because there is a lot of 3rd party content being transformed. I just don't want to use getResourceAsStream or some such in case it works differently. The println prints the whole document as I'd expect. – Daniel Gerson Dec 06 '15 at 21:09
  • Yeah. That should just work. Note it has nothing at all to do with the stream, though: if you just load that FXML file in the "normal" way (`TextFlow textFlow = FXMLLoader.load(getClass().getResource("inputFxml.fxml"));`) you get the same result. I'll see if I can figure out a workaround later. – James_D Dec 06 '15 at 21:24
  • Eventually openStream will be called on the URL, so you may find it's the same issue just in a different place. – Daniel Gerson Dec 06 '15 at 21:41
  • Note that there are [other bugs in this area](https://bugs.openjdk.java.net/browse/JDK-8088266) and perhaps that workaround will work here too. – Daniel Gerson Dec 06 '15 at 21:50

1 Answers1

0

So until a better solution is available, I opted for the hacky approach of inserting a markup string and then replacing it after the fxml has been loaded.

Not great but sufficient for my use case.

public void markNewLinesInXmlTextNodes(Node node) { //w3c node
    for(int i = 0 ; i < node.getChildNodes().getLength() ; i++) {
      Node child = node.getChildNodes().item(i);
      markNewLinesInXmlTextNodes(child);
    }
    if (node instanceof Element) {
      Element el = (Element) node;
      if (el.getTagName().toLowerCase().equals("text")) {
        el.setTextContent(el.getTextContent().replaceAll("\n", "_LINEBREAK_"));
      }
    }
  }

  public void addNewLinesInFxTextNodes(javafx.scene.Node node) {
    if (node instanceof javafx.scene.Parent) {
      javafx.scene.Parent parent = (javafx.scene.Parent) node;
      for(int i = 0 ; i < parent.getChildrenUnmodifiable().size() ; i++) {
        javafx.scene.Node child = parent.getChildrenUnmodifiable().get(i);
        addNewLinesInFxTextNodes(child);
      }
    }
    if (node instanceof javafx.scene.text.Text) {
      Text el = (Text) node;
      el.setText(el.getText().replaceAll("_LINEBREAK_", "\n"));
    }
  }
Daniel Gerson
  • 2,159
  • 1
  • 19
  • 29