zoukankan      html  css  js  c++  java
  • TextFlow with JavaFX 2

    http://sahits.ch/blog/?p=2372

    ————————————————————————————————————————————————————

    TextFlow with JavaFX 2

    When ever you want to display a large portion of text in your application the Text node is your friend. Text allows you to define a wrapping width and thereby allows nice multyline text without letting you bother about the line break.

    However what when your text is not that simple? It might contain portions that are differently formatted? Links? Or even icons?

    JavaFX 8 has a solution for this: TextFlow. If you are however stuck with JavaFX 2 for whatever reason, there is simple workaround: pack all the Nodes into a FlowPane and define the wrapping width to be the preferred width.

    public class DecoratedText extends FlowPane {
     
        private IntegerProperty wrappingWidth;
        private ReadOnlyObjectProperty<Font> font;
        public DecoratedText(Font font) {
            super(Orientation.HORIZONTAL);
            wrappingWidth = new SimpleIntegerProperty(this, "wrappingWidth", 0);
            getStylesheets().add(getClass().getResource(getClass().getSimpleName()+".css").toExternalForm());
            this.font = new SimpleObjectProperty<>(this, "font", font);
            prefWidthProperty().bindBidirectional(wrappingWidth);
        }
     
        /**
         * Append text. If the text represents a paragraph (indicated by '
    '), it
         * is not broken up into its parts.
         * @param text
         */
        public void append(String text) {
            if (text.endsWith("
    ")) {
                Text decoText = new Text(text);
                //decoText.getStyleClass().add("decoratedText-text");
                decoText.setFont(font.get());
                decoText.wrappingWidthProperty().bind(wrappingWidth);
                getChildren().add(decoText);
            } else {
                String[] parts = text.split(" ");
                for (String part : parts) {
                    Text decoText = new Text(part+" ");
                    //decoText.getStyleClass().add("decoratedText-text");
                    decoText.setFont(font.get());
                    getChildren().add(decoText);
                }
            }
        }
     
        /**
         * Append a control.
         * @param control
         */
        public void append(Node control) {
             getChildren().add(control);
        }
     
        public int getWrappingWidth() {
            return wrappingWidth.get();
        }
     
        public IntegerProperty wrappingWidthProperty() {
            return wrappingWidth;
        }
     
        public void setWrappingWidth(int wrappingWidth) {
            this.wrappingWidth.set(wrappingWidth);
        }
    }

    The idea here is to leave text blocks which represent a paragraph by themselves as Text node. However if the paragraph contains some other node, it is splitt up into words. Thereby delegating the wrapping functionality to the underlying FlowPane.

    While this solution here is not particularly sophisticated, it suffices my needs. Nevertheless I will replace it with TextFlow as soon as I migrate to Java 8.

    Schlagworte:

  • 相关阅读:
    linux中配置celery定时任务
    django2 将request.body 中的json字符串转换成字典
    在postman中各种填写参数的区别
    requests.Request 中参数data与json的区别
    Java中使用OpenSSL生成的RSA公私钥进行数据加解密
    openssl生成RSA密钥证书
    WKWebViewJavascriptBridge
    LeetCode实现 strStr()Swift
    LeetCode删除排序数组中的重复项Swift
    LeetCode合并两个有序链表Swift
  • 原文地址:https://www.cnblogs.com/cuizhf/p/3518945.html
Copyright © 2011-2022 走看看