javafx treetableview cell value not getting update

2020-07-24 05:19发布

问题:

Event after updating the data model javafx treetableview cell value not getting updated.

I am using the sample code here @ http://docs.oracle.com/javase/8/javafx/user-interface-tutorial/tree-table-view.htm (Example 15-2)

On click of button i m trying to update first item: employees.get(0).setName("Test");

Is there any trick using which treetableview can be updated?

回答1:

The example, somewhat strangely, returns a ReadOnlyStringWrapper wrapping the property values for the cell value factories. Thus instead of binding the value displayed in the column directly to the properties in the Employee class, it binds them to a new, read-only, property wrapping the value retrieved when updateItem(..) is called on the cell. This means it won't get updated when the underlying data is updated, but only if the updateItem(...) method is invoked on the cell. (I have no idea why they would do this.) So you should find, for example, that if you change the value, then collapse the root node in the TreeTableView and expand it again, that your new value is displayed after expanding the root (because this causes the cells' updateItem(...) methods to be invoked).

To make the cells update when the data is changed, bind the cell value directly to the property defined in the model (Employee) class:

empColumn.setCellValueFactory( param -> param.getValue().getValue().nameProperty());

and

emailColumn.setCellValueFactory( param -> param.getValue().getValue().emailProperty());