如何使用FXML和JavaFX动态创建TextFlow时保留新行字符

时间:2015-12-06 20:33:23

标签: java javafx fxml fxmlloader

以下代码动态创建一个TextFlow和Text,其中最初有新的行字符(可能是图像和其他窗格)。

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

但是,使用FXML的ByteArrayInputStream方法会删除所有新行。

尝试使用Windows和Unix的行尾字符,而不是那些应该会有所作为的,但是当你尝试不同的组合时,你就会达到这一点。

通常是recommended to use a BufferedReader来恢复新线路,但在这里我无法控制。其他加载方法似乎采用URL和资源链接,但这是一个被操纵的字符串。

示例

java代码:

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>

1 个答案:

答案 0 :(得分:0)

因此,在有更好的解决方案可用之前,我选择了插入标记字符串的hacky方法,然后在加载fxml之后替换它。

不是很好但足以满足我的用例。

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"));
    }
  }