JavaFx 2 使用单列创建 TableView [英] JavaFx 2 create TableView with single column

查看:44
本文介绍了JavaFx 2 使用单列创建 TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码创建一个只有一列的表:

I am trying to create a table with a single column using the following code :

TableView<String> table = new TableView<String>();
table.getColumns().clear();
table.getColumns().add(new TableColumn<String, String>("City Name"));
table.setItems(cityList);

但是我得到一个表格,其中包含城市名称"列,后跟一个空白列

However I get a table with the "City Name" column followed by a blank column

我是 JavaFx 的新手,所以可能有更好的方法来做到这一点.

I am new to JavaFx so there might be a better way of doing this.

推荐答案

我记得我过去曾尝试通过使用 css 属性来删除"空白列,但没有运气.解决方法是,
- 手动设置 cityColumn 的首选项宽度以覆盖整个空间:

I recall that tried to "remove" blank columns myself by playing with css properties in the past without luck. The workaround was either,
- set the pref width of the cityColumn to cover whole space manually:

TableColumn<String, String> cityColumn = new TableColumn<String, String>("City Name");
cityColumn.setPrefWidth(table.getPrefWidth() - 2);

-2 用于边框宽度.您也可以直接将列宽属性绑定到表格宽度属性,从而在调整表格宽度时自动更新列宽.请参阅此答案https://stackoverflow.com/a/10152992/682495.
或者,
- 将列大小调整策略设置为 CONSTRAINED_RESIZE_POLICY:

-2 for border widths. Also you can bind column width property to table width property directly, resulting the col width is updated automatically when the table width is resized. See this answer https://stackoverflow.com/a/10152992/682495.
Or,
- set the column resize policy to CONSTRAINED_RESIZE_POLICY:

table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY);

这篇关于JavaFx 2 使用单列创建 TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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