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 '\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的更多相关文章
- javafx 聊天室WeChat
[toc] 功能和特性 基于socket实现的c/s架构的的通信 服务器和客户心跳连接 gson实现的消息通信机制 注册及登录 支持私聊和群聊. 动态更新用户列表以及用户消息提示 支持emoji表情, ...
- 问题记录:JavaFx 鼠标滑轮滚动事件监听!
问题描述: 在listview的item里面添加鼠标拖拽排序功能.代码如下: setOnMouseDragged(event -> { //设定鼠标长按0.3秒后才可拖拽 防止误操作 isCan ...
- JavaFx客户端服务器C/S架构搭建
客户端获取服务器端软件更新版本方法: package com.platform.ui.update; import java.io.BufferedInputStream; import java.i ...
- JavaFX 教程资料收集
1. JavaFX中文资料 http://www.javafxchina.net/blog/docs/tutorial1/ 2. JavaFX入门教程 http://www.xrpmoon.com/c ...
- 在 linux 上部署并运行 JavaFX 项目
环境 redhat 6.4.eclipse安装JavaFX插件 项目详情及代码参见 在linux上配置并创建JavaFX项目 ,该部署即此文章中项目的部署 配置build.fxbuild 生成buil ...
- 在linux上配置并创建JavaFX项目
本环境为linux配置,因为这里的JavaFX项目是为定制Oracle监控工具而写的.现Oracle已收购Java好几年,用它自己的产生监控自己的东西还是很兼容的.此处Eclipse 为4.5版本. ...
- javafx之登陆界面的跳转
界面布局用到的是fxml而非纯java代码,工具是javafx sence builder 账号:account 密码:password 登陆成功: 可以点击退出登陆返回到登陆页面 工程目录: pac ...
- JAVAFX纯手写布局
主页面效果: 第一栏的效果: 工程目录: package MessageBean; /** * * @author novo */ public class Message { private Str ...
- javafx之HTTP协议交互
javafx端要获取获取如下信息: 服务器端获取的数据: javafx客户端发送的数据以及获取的数据: 工程目录: package Httputil; import IPsite.IPaddress; ...
随机推荐
- HTML5本地缓存localStorage和sessionStorage的操作方法收集
说明: Web Storage 包含如下两种机制: sessionStorage 为每一个给定的源(given origin)维持一个独立的存储区域,该存储区域在页面会话期间可用(即只要浏览器处于打开 ...
- Zookeeper api增删改查节点
Exists - 检查Znode的存在 ZooKeeper类提供了 exists 方法来检查znode的存在.如果指定的znode存在,则返回一个znode的元数据.exists方法的签名如下: ex ...
- WebLogic Server的密码复杂性规则设置
WebLogic Server默认的密码复杂性规则是长度为8位以上,必须包含数字和字母,但对于很多企业的安全而言是不足够的,因此WebLogic Server在Security模块中提供了Passwo ...
- 如何看待Linux操作系统的用户空间和内核空间
作为中央核心处理单元的CPU,除了生产工艺的不断革新进步外,在处理数据和响应速度方面也需要有权衡.稍有微机原理基础的人都知道Intel X86体系的CPU提供了四种特权模式ring0~ring3,其中 ...
- HDFS 中向 DataNode 写入数据失败了怎么办
https://blog.csdn.net/HeatDeath/article/details/79012258 http://wenda.chinahadoop.cn/question/3323 h ...
- win7安装centos7双系统
采用硬盘安装 前景 打算用U盘安装,但是u盘是FAT32格式限制了文件4g大小,我官网下的iso镜像大于4g,只好采用硬盘安装. 其实U盘安装是最方便的,网上很多教程用UltraISO软件把U盘直接作 ...
- seo关键字优化条例
SEO 第一: 标题关键字分析 分析和选择行业热门的关键字,并合理的应用于网站标题内及分布到各栏目页面和内页. 其实个人觉得标题.内容.以及与内容相关性链接必须要足.还有就是出现的层次感,例如: a) ...
- js 队列和事件循环
1.示例代码 <!DOCTYPE html> <html lang="zh"> <head> <meta charset="UT ...
- openerp所用QWEB2的调试笔记
[1] 调式qweb模板时, 可以脱离openerp环境 阅读一下openerp目录 qweb目录中的几个html文件,可以作为起步 在浏览器下, 可以这样运行这些文件 http://127.0.0. ...
- LINPACK測试
1简单介绍 LINPACK是线性系统软件包(Linear system package) 的缩写. Linpack如今在国际上已经成为最流行的用于測试高性能计算机系统浮点性能的benchmark.通过 ...