博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TextFlow with JavaFX 2
阅读量:6242 次
发布时间:2019-06-22

本文共 2835 字,大约阅读时间需要 9 分钟。

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 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: . 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;    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 '\n'), it     * is not broken up into its parts.     * @param text     */    public void append(String text) {        if (text.endsWith("\n")) {            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:

转载地址:http://ppsia.baihongyu.com/

你可能感兴趣的文章
SpringCloud(第 021 篇)Zuul 的过滤器 ZuulFilter 的使用
查看>>
JavaScript笔记——闭包
查看>>
gRPC 初探
查看>>
SpringBoot非官方教程 | 第六篇:SpringBoot整合mybatis
查看>>
dataguard备库出现GAP修复
查看>>
OOD、DIP、IOC、DI、依赖注入容器(即 控制反转容器,IOC Container)
查看>>
Linux常用命令
查看>>
Grub4Dos 手动引导指令
查看>>
C# 有道API翻译 查询单词详细信息
查看>>
android 录像提示音问题
查看>>
纯CSS制作各种图形(多图预警)
查看>>
程序员如何获取招聘信息
查看>>
水平滑动,记录当前状态、利用浏览器原生播放器播放视频和vue-video-player视频播放插件、基于museUI的音频播放和vue-player插件实现音频播放...
查看>>
Kaa IoT平台学习(一)
查看>>
深入了解JVM虚拟机8:Java的编译期优化与运行期优化
查看>>
使用Nagios打造专业的业务状态监控
查看>>
单例模式(java&iOS)
查看>>
重拾Java(8)-反射
查看>>
有没有可以共享的桌面便签?
查看>>
Mars说光场(3)— 光场采集
查看>>