JavaFx 2使用单列创建TableView [英] JavaFx 2 create TableView with single column
问题描述
我正在尝试使用以下代码创建一个包含单个列的表:
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
的pref宽度以手动覆盖整个空间:
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
。您还可以直接将列宽属性绑定到表宽度属性,从而在调整表宽度时自动更新col宽度。请参阅此答案 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屋!