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; ...
随机推荐
- 事务的实现就是利用数据库锁(行锁,表锁等),且db上锁,都是在操作之前上锁
悲观锁(Pessimistic Lock), 顾名思义,就是很悲观,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会block直到它拿到锁.传统的关系型数据 ...
- Apache -Common-lang包使用
原文:http://weigang-gao.iteye.com/blog/2188739 ArrayUtils – 用于对数组的操作,如添加.查找.删除.子数组.倒序.元素类型转换等: BitFiel ...
- [干货]2017已来,最全面试总结——这些Android面试题你一定需要
地址.http://blog.csdn.net/xhmj12/article/details/54730883 相关阅读: 吊炸天!74款APP完整源码! [干货精品,值得收藏]超全的一线互联 ...
- IP windows相关
nbtstat: 假设 我们 通过net view 获取了 局域网内一些计算机名 如上标红的计算机名称 如何 才能获取计算机的ip呢? 接下来使用 nbtstat -a 列出远程机器的名称表: ...
- [转载]Ubuntu 14.04中root 密码忘记解决方法
Ubuntu 14.04中root 密码忘记解决方法 方法一: 如果用户具有sudo权限,那么直接可以运行如下命令: #sudo su root #passwd #更改密码 或者直接运行sudo ...
- Oracle使用row_number()函数查询时增加序号列
使用Oracle自带的row_number()函数能够实现自动增加序号列的要求,但是同时引发一个问题,如果我们查询出来的数据需要使用Order By排序的话,那么我们会发现新增加的序号列是乱序的,它会 ...
- [Python爬虫] 之十八:Selenium +phantomjs 利用 pyquery抓取电视之家网数据
一.介绍 本例子用Selenium +phantomjs爬取电视之家(http://www.tvhome.com/news/)的资讯信息,输入给定关键字抓取资讯信息. 给定关键字:数字:融合:电视 抓 ...
- PHPCMS V9管理员password忘记怎样改动
一般的虚拟主机商都提供了PHPmyAdmin,选择你站点数据库.然后选择v9_admin这个表. 编辑 password,变成:fa3250300be9b7ab0848257f3cbb06e7 enc ...
- numpy基础知识
官网简介: http://www.numpy.org/ ndarry基本属性 ndarry是Numpy中的N维数组对象(N dimentional arrya,ndarray) ndarry中所有的元 ...
- Spark(四) -- Spark工作机制
一.应用执行机制 一个应用的生命周期即,用户提交自定义的作业之后,Spark框架进行处理的一系列过程. 在这个过程中,不同的时间段里,应用会被拆分为不同的形态来执行. 1.应用执行过程中的基本组件和形 ...