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:

  • 相关阅读:
    Windows Phone 7 中常用Task
    设置Highchart柱子最大宽度( 让 highcharts支持maxPointWidth属性)
    Asp.Net MVC 使用FileResult导出Excel数据文件
    js获取网页高度
    使用window.addEventListener 和 window.attachEvent 判断浏览器
    slimscroll滚动条插件简单用法
    js中如何快速获取数组中的最大值最小值
    js 判断浏览器类型
    python使用ldap进行用户认证
    关于go声明切片的一些疑问
  • 原文地址:https://www.cnblogs.com/cuizhf/p/3518945.html
Copyright © 2011-2022 走看看