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. delphi报警声音 Beep、MessageBeep 和 Windows.Beep

      转自:http://blog.csdn.net/yunqian09/article/details/5554527 我的办法,增加一个timer 设置间隔100ms,通过timer的使能否,控制报 ...

  2. 【mybatis】mybatis中update更新原来的值加1

    示例代码: floor的值 = floor原来的值+要更新的差距值 <update id="updateFloor" parameterType="com.pise ...

  3. 安装dubbo-admin报错 URIType BeanCreationException

    安装dubbo-admin报错 URIType BeanCreationException 学习了:https://blog.csdn.net/lsm135/article/details/52725 ...

  4. iOS开发中经常使用的Xcode插件

    1.全能搜索家CodePilot 2.0 你要找的是文件?是目录?是代码?Never Mind,CMD+SHIFT+X调出CodePilot,输入不论什么你想到搜的东西吧! 想搜appFinishLa ...

  5. 补番计划 (长沙理工大学第十一届程序设计竞赛)(双端队列+set容器+string)

    补番计划 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submissi ...

  6. ssh之<context:component-scan base-package="com.xx" />

    <context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能, 同时还启用了注释驱动自动注入的功能 ( 即还隐式地在内部注册了 A ...

  7. $ gulp watch 运行出错解决方法

    $ gulp watch 运行出错解决方法   $ gulp watch     如果你出现了如下报错信息: gulp-notify: [Laravel Elixir] Browserify Fail ...

  8. vue 项目中 自定义 webpack 的 配置文件(webpack.config.babel.js)

    webpack.config.babel.js,这样命名是想让webpack在编译的时候自动识别es6的语法,现在貌似不需要这样命名了,之前用webpack1.x的时候貌似是需要的 let path ...

  9. C++第4次实验(基础班)—循环结构程序设计

    此次上机中的4个题目项目6.项目7(选1)必做.其他2两题可从剩下的项目中选,也可从项目7中选. [项目1:利用循环求和]求1000以内全部偶数的和(答案:250500) 要求:请编出3个程序来,分别 ...

  10. 【DB2】NOT IN使用中的大坑

    1.环境准备 ------建表TB DROP TABLE TB; CREATE TABLE TB ( ID INTEGER, LEVEL_DETAIL ) ); INSERT INTO TB (ID, ...