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.
TableView is a component that is used to create a table populate it, and remove items from it. You can create a table view by instantiating thejavafx. scene. control.
The JavaFX TableView enables you to specify the default sort order of a TableView. The items in the TableView will be sorted according to this order - until the user clicks some TableColumn headers and changes the sort order. The default sort order consists of an ObservableList of TableColumn instances.
public PropertyValueFactory(String property) Creates a default PropertyValueFactory to extract the value from a given TableView row item reflectively, using the given property name. Parameters: property - The name of the property with which to attempt to reflectively extract a corresponding value for in a given object.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With