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. 使用FluentValidation来进行数据有效性验证

    之前我介绍过了使用系统自带的Data Annotations来进行数据有效性验证,今天在CodePlex上逛的时候,发现了一个非常简洁好用的库:FluentValidation 由于非常简洁,就直接拿 ...

  2. 简单php连接数据库作操作

    1.近期稳定版本 <?php header('Content-Type: application/json'); $output = []; $host = ''; //MySQL服务器地址 $ ...

  3. iOS:自定义代码块{ }

    1.Xcode本身带有编码常用的代码块可供使用,如下图 调用方法: (1)直接拖到代码区域中: (2)使用快捷键,键入 “while”, Xcode就会出现自动完成的提示 也可以自定义自己常用的代码块 ...

  4. Cobbler安装CentOS 7网卡命名修改

    准备上线CentOS 7.x,在Cobbler上导入后,发现网卡名称变成了eno1这样的,好吧,那就添加两个内核参数上去,让它变回eth0. cobbler profile edit --name=C ...

  5. pgmagick,pil不保存图片并且获取图片二进制数据记录

    PIL和pgmagick都是python中图像处理的库,只不过PIL功能更强大 pgmagick和PIL中对数据进行调整后经常需要调用write或者save方法保存图片,然后在读取图片的内容,这样很麻 ...

  6. angular—— Dynamic Templates

    原文:http://davidcai.github.io/blog/posts/router-dynamic-templates/ ui-router : templateProvider vs te ...

  7. Java Web---登录验证和字符编码过滤器

    什么是过滤器? 在Java Web中,过滤器即Filter.Servlet API中提供了一个Filter接口(javax.servlet.Filter).开发web应用时,假设编写的Java类实现了 ...

  8. &lt;十&gt;读&lt;&lt;大话设计模式&gt;&gt;之观察者模式

    观察者模式也是比較简单的一种模式,可能从名字上理解无法明确,但真正理解其含义之后就非常easy了,说实话在自己来发的项目中自己也用到过.仅仅只是不知道它叫观察者罢了,仅仅要懂面向对象的对继承多态理解非 ...

  9. 表单提交post和get方法区别

    表象不同,get把提交的数据url可以看到,post看不到 原理不同,get 是拼接 url, post 是放入http 请求体中 提交数据量不同,get最多提交2k数据,浏览器的限制.post理论上 ...

  10. IDEA 找不到maven编译命令操作

    找到idea左上角菜单View>Tool Windows>Maven projects.