sameiksha
sameiksha

Reputation: 25

How to set list of map into javafx table view

The following code snippets show the declarations of the fields and how I set up the table.

Please tell me what went wrong. The data is not getting displayed.

@FXML
private TableColumn<Map<String, Object>, String> productNameCol ;

@FXML
private TableColumn<Map<String,Object>, String> requestedBarcodeCol;

@FXML
private TableColumn<Map<String,Object>, String> pickedBarcodeCol;
productNameCol.setCellValueFactory(new PropertyValueFactory<Map<String,Object>,String>("ProductName"));
requestedBarcodeCol.setCellValueFactory(new PropertyValueFactory<Map<String,Object>,String>("ActualBarcode"));
pickedBarcodeCol.setCellValueFactory(new PropertyValueFactory<Map<String,Object>,String>("PickedBarcode"));
requestedQuantityCol.setCellValueFactory(new PropertyValueFactory<Map<String,Object>,String>("ActualQuantity"));
actualQuantityCol.setCellValueFactory(new PropertyValueFactory<Map<String,Object>,String>("PickedQuantity"));


unMatchDataTableView.setItems(FXCollections.observableArrayList(unmatchedBarcodeMap));

Upvotes: 0

Views: 309

Answers (1)

fabian
fabian

Reputation: 82461

Since the question does not contain a MCVE, I've got to make some assumptions; the solution I present may not work without modifications. Those are my assumptions:

  • unmatchedBarcodeMap is non-empty
  • The controller is properly used when loading the fxml
  • The fields contain the instances used in the TableView that is displayed on screen

PropertyValueFactory uses getters or property getters to retrieve values. These are not available for Map:

Let <property> denote the string passed to the PropertyValueFactory and <Property> the same string but with the first char converted to upper case:

PropertyValueFactory looks for a method named <property>Property that returns a ObservableValue containing the value to be displayed in the column first. If such a method exists, the result of invoking the method on the row item is returned from the call method. If this method is not available, PropertyValueFactory looks for a method named get<Property>; If this method is available, the result of invoking the method for the row item is wrapped in a ObservableValue object and returned from the call method. Otherwise null is returned resulting in an empty cell.

You need to use MapValueFactory instead of PropertyValueFactory to retrieve values from the Maps:

productNameCol.setCellValueFactory((Callback) new MapValueFactory("ProductName"));
...

This displays the result of calling using rowItem.get("ProductName") in the productNameCol column.

Upvotes: 1

Related Questions