JavaFx 2 creates a single column tableview

I am trying to create a single column table 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 an empty column

I'm new to JavaFx, so there may be a better way to do this.

+7
source share
1 answer

I remember trying to β€œdelete” empty columns while playing with css properties in the past with no luck. The workaround was either
- set the width of the cityColumn prefix to cover the entire space manually:

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

-2 for the width of the borders. You can also directly associate the column width property with the table width attribute, as a result, the column width is automatically updated when the table is resized. See this answer at https://stackoverflow.com/a/464628/
Or
- Set the column resizing policy to CONSTRAINED_RESIZE_POLICY :

 table.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); 
+13
source

All Articles