Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
364 views
in Technique[技术] by (71.8m points)

java - 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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...