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 '\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: JavaFX

TextFlow with JavaFX 2的更多相关文章

  1. javafx 聊天室WeChat

    [toc] 功能和特性 基于socket实现的c/s架构的的通信 服务器和客户心跳连接 gson实现的消息通信机制 注册及登录 支持私聊和群聊. 动态更新用户列表以及用户消息提示 支持emoji表情, ...

  2. 问题记录:JavaFx 鼠标滑轮滚动事件监听!

    问题描述: 在listview的item里面添加鼠标拖拽排序功能.代码如下: setOnMouseDragged(event -> { //设定鼠标长按0.3秒后才可拖拽 防止误操作 isCan ...

  3. JavaFx客户端服务器C/S架构搭建

    客户端获取服务器端软件更新版本方法: package com.platform.ui.update; import java.io.BufferedInputStream; import java.i ...

  4. JavaFX 教程资料收集

    1. JavaFX中文资料 http://www.javafxchina.net/blog/docs/tutorial1/ 2. JavaFX入门教程 http://www.xrpmoon.com/c ...

  5. 在 linux 上部署并运行 JavaFX 项目

    环境 redhat 6.4.eclipse安装JavaFX插件 项目详情及代码参见 在linux上配置并创建JavaFX项目 ,该部署即此文章中项目的部署 配置build.fxbuild 生成buil ...

  6. 在linux上配置并创建JavaFX项目

    本环境为linux配置,因为这里的JavaFX项目是为定制Oracle监控工具而写的.现Oracle已收购Java好几年,用它自己的产生监控自己的东西还是很兼容的.此处Eclipse 为4.5版本. ...

  7. javafx之登陆界面的跳转

    界面布局用到的是fxml而非纯java代码,工具是javafx sence builder 账号:account 密码:password 登陆成功: 可以点击退出登陆返回到登陆页面 工程目录: pac ...

  8. JAVAFX纯手写布局

    主页面效果: 第一栏的效果: 工程目录: package MessageBean; /** * * @author novo */ public class Message { private Str ...

  9. javafx之HTTP协议交互

    javafx端要获取获取如下信息: 服务器端获取的数据: javafx客户端发送的数据以及获取的数据: 工程目录: package Httputil; import IPsite.IPaddress; ...

随机推荐

  1. IIS8集成模式下打开静态资源被aspx处理程序处理,StaticFileModule失效问题分析

    问题描述: 打开js,css,jpg之类的静态资源文件触发了asp.net mvc的权限认证,并不是直接返回静态内容 问题分析: StaticFileModule 失效 ,可能是文件权限问题 问题解决 ...

  2. winform treeview 绑定文件夹和文件

    转载:http://www.cnblogs.com/zhbsh/archive/2011/05/26/2057733.html #region treeview 绑定文件夹和文件 /// <su ...

  3. Swift,下标简化方法的调用

    在类(class)当中采用subscript的方法直接用下标 class a{ func b(number:Int)->Int{ return number } subscript(number ...

  4. linux/mac系统的软链接文件与硬链接文件

    1.硬连接只能使用在文件上,不可以使用在文件夹上.至于文件前面的硬链接数字的含义如下: 如图标注区,为硬连接的数量,文件前的数字1表示没有硬链接.文件夹前面的数字至少是2,含义是这个文件夹是空文件夹, ...

  5. 深入理解JavaScript中的函数操作——《JavaScript忍者秘籍》总结

    匿名函数 对于什么是匿名函数,这里就不做过多介绍了.我们需要知道的是,对于JavaScript而言,匿名函数是一个很重要且具有逻辑性的特性.通常,匿名函数的使用情况是:创建一个供以后使用的函数.简单的 ...

  6. Accessing Report Server using Report Server Web Service - Microsoft SQL Server 2008R2

    Today I am going to write about how to access SQL Server Report Server through Report Server Web ser ...

  7. nyoj237 游戏高手的烦恼(最小点覆盖)

    题目237 题目信息 pid=237" style="text-decoration:none; color:rgb(55,119,188)">执行结果 本题排行 ...

  8. 倍福TwinCAT(贝福Beckhoff)应用教程11.1 TwinCAT应用小程序1 贝福IO模块介绍

    EL1002,EL1004,EL1008都是数字输入模块(2个点,4个点,8个点),输入高的范围是15V到30V,低的范围是-3V到5V         EL2002,EL2004,EL2008都是数 ...

  9. Python爬取豆瓣《复仇者联盟3》评论并生成乖萌的格鲁特

    代码地址如下:http://www.demodashi.com/demo/13257.html 1. 需求说明 本项目基于Python爬虫,爬取豆瓣电影上关于复仇者联盟3的所有影评,并保存至本地文件. ...

  10. Python 的条件语句和循环语句

    一.顺序结构 顺序结构是最简单的一种程序结构,程序按照语句的书写次序自上而下顺序执行. 二.分支控制语句 Python条件语句是通过一条或多条语句的执行结果(True或者False)来决定执行的代码块 ...