JavaFX 2自动列宽 [英] JavaFX 2 Automatic Column Width

查看:47
本文介绍了JavaFX 2自动列宽的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个JavaFX 2表,该表显示了人员的联系方式,让我们想象一下有三列:名字,姓氏和电子邮件地址.当我的应用程序启动时,它会在表中填充有关系统中已有人员的几行数据.

I have a JavaFX 2 table that is displaying contact details for people, lets imagine there are three columns: first name, last name and email address. When my application starts it populates the table with several rows of data about the people already in the system.

问题是列宽都相同.大多数情况下,名字和姓氏都会完整显示,但电子邮件地址会被裁剪.用户可以双击标题中的分隔符来调整列的大小,但这很快就会变得乏味.

The problem is that the column widths are all the same. Most of the time the first and last name is displayed in full but the email address is getting clipped. The user can double click the divider in the header to resize the column but that will become tedious quickly.

表已预先填充后,我想以编程方式调整所有列的大小以显示它们包含的数据,但我不知道如何实现此目的.我可以看到我可以调用col.setPrefWidth(x),但这并没有真正的帮助,因为我不得不猜测宽度.

Once the table has been pre-populated I would like to programatically resize all the columns to display the data they contain but I can't figure out how to achieve this. I can see that I can call col.setPrefWidth(x) but that doesn't really help as I would have to guess the width.

推荐答案

如果您知道列的总数.您可以在表格视图的宽度之间分配列的宽度:

If your total number of columns are pre-known. You can distribute the column widths among the tableview's width:

nameCol.prefWidthProperty().bind(personTable.widthProperty().divide(4)); // w * 1/4
surnameCol.prefWidthProperty().bind(personTable.widthProperty().divide(2)); // w * 1/2
emailCol.prefWidthProperty().bind(personTable.widthProperty().divide(4)); // w * 1/4

在此代码中,调整表格视图的大小时,列的宽度比例保持同步,因此您无需手动进行操作.此外,surnameCol占用表视图宽度的一半空间.

In this code, the width proportions of columns are kept in sync when the tableview is resized, so you don't need to do it manually. Also the surnameCol takes the half space of the tableview's width.

这篇关于JavaFX 2自动列宽的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆