Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 410
Ayub Malik Avatar asked Jun 14 '12 12:06

Ayub Malik


People also ask

How to set data in TableView in JavaFX?

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.

What is TableView JavaFX?

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.

What is PropertyValueFactory in JavaFX?

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.


1 Answers

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);
like image 92
Uluk Biy Avatar answered Oct 19 '22 22:10

Uluk Biy