JavaFX技巧22:“自动调整大小(树)”表列

JavaFX “缺少功能调查”中提到的“缺少功能”的第一件事就是能够自动调整表/树表中的列大小。 没错,没有公共API是正确的,但是当您密切关注时,您会注意到JavaFX内部一定有执行此操作的代码,因为用户可以通过双击分隔线自动调整列的大小。在该列与右侧的下一列之间。

但是像大多数人一样,我觉得这对我的代码还不够好。 我想要一个FlexGanttFX的API,该API允许用户自动调整甘特图内一列或所有列的大小。 因此,我搜索了隐藏在树表或树表皮肤某处(实际上不记得在哪里)的代码,并在类中进行了一些细微修改以重新使用它。

以下是这项工作的结果。 它以TreeTableView而不是TableView为目标,但是使它适用于标准表很简单。 只需将所有TreeTableColumn出现替换为TableColumn即可 。 请注意,调整所有行的大小可能会对性能产生严重影响,因此您可能必须通过maxRows参数来限制要用于计算的行

/*** This method will resize all columns in the tree table view to ensure that* the content of all cells will be completely visible. Note: this is a very* expensive operation and should only be used when the number of rows is* small.** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumns() {resizeColumns(-1);}/*** This method will resize all columns in the tree table view to ensure that* the content of all cells will be completely visible. Note: this is a very* expensive operation and should only be used with a small number of rows.** @param maxRows*            the maximum number of rows that will be considered for the*            width calculations** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumns(int maxRows) {for (TreeTableColumn<R, ?> column : getTreeTable().getColumns()) {resizeColumn(column, maxRows);}}/*** This method will resize the given column in the tree table view to ensure* that the content of the column cells will be completely visible. Note:* this is a very expensive operation and should only be used when the* number of rows is small.** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumn(TreeTableColumn<R, ?> column) {resizeColumn(column, -1);}/*** This method will resize the given column in the tree table view to ensure* that the content of the column cells will be completely visible. Note:* this is a very expensive operation and should only be used when the* number of rows is small.** @see #resizeColumn(TreeTableColumn, int)*/public final void resizeColumn(TreeTableColumn<R, ?> tc, int maxRows) {final TreeTableColumn col = tc;List<?> items = getItems();if (items == null || items.isEmpty()) {return;}Callback cellFactory = tc.getCellFactory();if (cellFactory == null) {return;}TreeTableCell<R, ?> cell = (TreeTableCell<R, ?>) cellFactory.call(tc);if (cell == null) {return;}// set this property to tell the TableCell we want to know its actual// preferred width, not the width of the associated TableColumnBasecell.getProperties().put("deferToParentPrefWidth", Boolean.TRUE); //$NON-NLS-1$// determine cell paddingdouble padding = 10;Node n = cell.getSkin() == null ? null : cell.getSkin().getNode();if (n instanceof Region) {Region r = (Region) n;padding = r.snappedLeftInset() + r.snappedRightInset();}TreeTableRow<R> treeTableRow = new TreeTableRow<>();treeTableRow.updateTreeTableView(treeTableView);int rows = maxRows == -1 ? items.size(): Math.min(items.size(), maxRows);double maxWidth = 0;for (int row = 0; row < rows; row++) {treeTableRow.updateIndex(row);treeTableRow.updateTreeItem(treeTableView.getTreeItem(row));cell.updateTreeTableColumn(col);cell.updateTreeTableView(treeTableView);cell.updateTreeTableRow(treeTableRow);cell.updateIndex(row);if ((cell.getText() != null && !cell.getText().isEmpty())|| cell.getGraphic() != null) {getChildren().add(cell);cell.impl_processCSS(false);double w = cell.prefWidth(-1);maxWidth = Math.max(maxWidth, w);getChildren().remove(cell);}}// dispose of the cell to prevent it retaining listeners (see RT-31015)cell.updateIndex(-1);// RT-23486double widthMax = maxWidth + padding;if (treeTableView.getColumnResizePolicy() == TreeTableView.CONSTRAINED_RESIZE_POLICY) {widthMax = Math.max(widthMax, tc.getWidth());}tc.impl_setWidth(widthMax);}

翻译自: https://www.javacodegeeks.com/2015/12/javafx-tip-22-autosize-tree-table-columns.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/355218.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

aesmiyao php_PHP使用AES,ECB模式块和PKCS5Padding生成对称密钥

首先,要对你需要即兴创作的输入做PKCS#5填充&#xff1a;// source: http://php.net/manual/en/ref.mcrypt.php#69782function pkcs5_pad($text, $blocksize){$pad $blocksize - (strlen($text) % $blocksize);return $text . str_repeat(chr($pad), $pad);}然后选择你的算法并…

解决复合主键

解决复合主键使用IdClass(BzdmKey.class)注解 package entity.po;import javax.persistence.*;/*** Package main.java.pojo* Description BZDM对象的pojo类* Author zhaohuaqing*/ Entity Table(name "ts_bzdm") IdClass(BzdmKey.class) public class Bzdm {/*** …

【数据挖掘导论】——数据质量

数据质量数据挖掘使用的数据一般是为其它用途收集或者收集的时候还没有明白目的。因此数据经常不能在数据的源头控制质量。为了避免数据质量的问题&#xff0c;所以数据挖掘着眼于两个方面&#xff1a;数据质量问题的检測和纠正&#xff08;数据清理&#xff09;&#xff1b;使…

rocksdb原理_[转]Rocksdb Compaction原理

概述compaction主要包括两类&#xff1a;将内存中imutable 转储到磁盘上sst的过程称之为flush或者minor compaction&#xff1b;磁盘上的sst文件从低层向高层转储的过程称之为compaction或者是major compaction。对于myrocks来说&#xff0c;compaction过程都由后台线程触发&am…

P2216 [HAOI2007]理想的正方形(二维RMQ)

题目描述 有一个a*b的整数组成的矩阵&#xff0c;现请你从中找出一个n*n的正方形区域&#xff0c;使得该区域所有数中的最大值和最小值的差最小。 输入输出格式 输入格式&#xff1a; 第一行为3个整数&#xff0c;分别表示a,b,n的值 第二行至第a1行每行为b个非负整数&#xff0…

MD5加密

MD5加密package common.util;import java.math.BigInteger; import java.security.MessageDigest;/*** Package main.java.utils* Description 加密* Author zhaohuaqing*/ public class MD5 {public static final String KEY_MD5 "MD5";/*** param inputStr 输入的…

jrockit_JRockit JRCMD教程

jrockit本文将为您提供概述和教程&#xff0c;说明如何使用jrcmd工具对JRockit Java Heap问题进行初始分析和问题隔离。 将来的文章中将介绍使用JRockit任务控制和堆转储分析&#xff08;仅JRockit R28 版&#xff09;的更深入的分析和教程。 有关JRockit Java堆空间的快速概述…

jQuery 事件 - ready() 方法

jQuery 事件 - ready() 方法当 DOM&#xff08;文档对象模型&#xff09; 已经加载&#xff0c;并且页面&#xff08;包括图像&#xff09;已经完全呈现时&#xff0c;会发生 ready 事件。 1.语法1 $(document).ready(function)2.语法2 $().ready(function)3.语法3 $(funct…

axios vue 加载效果动画_vue中使用axios拦截器实现数据加载之前的loading动画显示 @劉䔳...

首先新建一个 loading.vue组件&#xff0c;写loading动画效果.loader {width: 100%;height: 100%;display: flex;align-items: center;justify-content: center}-webkit-keyframes loading{50% {transform: scale(.4);opacity: .3}100% {transform: scale(1);opacity: 1}}.load…

Spark学习笔记(7)---Spark SQL学习笔记

Spark SQL学习笔记 Spark SQL学习笔记设计到很多代码操作&#xff0c;所以就放在github, https://github.com/yangtong123/RoadOfStudySpark/blob/master/src/com/spark/sql/Readme.md其中包括了对Spark2.0的新特性的介绍&#xff0c;包括SparkSession, DataSet等转载于:https:…

性能实战(一) --- clock_gettime造成系统整体cpu过高定位过程

问题背景 有一台linux服务器测试环境cpu经常到达80%,造成系统卡顿,部分功能不可用. 分析步骤 1.使用perf制作cpu火焰图 通过制作cpu火焰图,发现很多进程都存在大量的clock_gettime系统调用. 2. 使用bcc工具funclatency`进一步查看clock_gettime的调用次数 # /usr/share/bc…

如果今天设计了Java:同步接口

Java已经走了很长一段路。 很长的路要走。 它带有早期设计决策中的所有“垃圾”。 一遍又一遍后悔的一件事是&#xff0c; 每个对象&#xff08;可能&#xff09;都包含一个监视器 。 几乎没有必要这样做&#xff0c;并且最终在Java 5中纠正了该缺陷&#xff0c;当时引入了新的…

简单Map缓存

简单Map缓存/*** 部门代码对应的部门名称*/private static Map<String,String> mapBmmc new HashMap<String, String>();/*** 性别性别代码对应的性别名称*/private static Map<String,String> mapMc new HashMap<String, String>();/*** descriptio…

sw二次开发 python_基于C#的SolidWorks二次开发.doc

摘要&#xff1a;气动电阻点焊钳已经被各大汽车制造厂商广泛运的用于汽车焊接工艺中。它以无污染、压力稳定、动作敏捷等优点逐步替代了国内常见的液压传动焊钳&#xff0c;改变了液压传动滞缓的现象&#xff0c;从而达到了焊接循环的要求。本次毕业设计中&#xff0c;设计者使…

玩透个人所得税

每次拿着工资条的时候&#xff0c;总有个代扣个税这么一项&#xff0c;不知道你们有没有想过这到底是怎样计算得出来的。下面我就给你们普及一下这个知识。 个人所得税 个人所得税是对个人&#xff08;自然人&#xff09;取得的各项所得征收的一种所得税。个人所得税…

@Value和Hibernate问题

Value和Could not obtain transaction-synchronized Session for current thread1.说明 Value(“#{}”) 表示SpEl表达式通常用来获取bean的属性&#xff0c;或者调用bean的某个方法。当然还有可以表示常量。 2.出现的问题 Caused by: org.hibernate.HibernateException: Cou…

u32转换bool类型_4.29.类型转换

类型转换casting-between-types.mdcommit 6ba952020fbc91bad64be1ea0650bfba52e6aab4Rust&#xff0c;和它对安全的关注&#xff0c;提供了两种不同的在不同类型间转换的方式。第一个&#xff0c;as&#xff0c;用于安全转换。相反&#xff0c;transmute允许任意的转换&#xf…

用原生JS读写CSS样式的方法总结

一、可以通过DOM节点对象的style对象(即CSSStyleDeclaration对象)来读写文档元素的CSS样式如&#xff1a;var elm document.getElementById(test);elm.style.color black;二、通过Element对象的getAttribute()、setAttribute()、removeAttribute()直接读写style属性如&#x…

html5开发ria_用于RIA的JavaFX 2与HTML5

html5开发ria这些天来&#xff0c;我们正在启动一个新项目&#xff0c;以实现Rich Internet Application&#xff08;RIA&#xff09; 。 第一个问题是&#xff1a;我们应该使用哪些技术和框架&#xff1f; 后端将是Java或其他现代JVM语言&#xff0c;因为我们是经验丰富的Java…

js里面拼接代码和使用ModelAndView

js里面拼接代码和使用ModelAndView1.js里面拼接代码 <tr><td class"tdTitle">性别</td><td class"tdCont"><select name"yhxb" id"yhxb" class"inputSel" style"width: 100px"><…