2

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?

HemantS
  • 208
  • 3
  • 11
  • Can you show some code: in particular the cell value factories? The factories in the example look wrong to me... – James_D May 08 '14 at 14:48
  • new TreeTableColumn<>("Employee"); empColumn.setPrefWidth(150); empColumn.setCellValueFactory( (TreeTableColumn.CellDataFeatures param) -> new ReadOnlyStringWrapper(param.getValue().getValue().getName()) ); – HemantS May 08 '14 at 15:54
  • I m using the code there in example 15-2 – HemantS May 08 '14 at 15:54

1 Answers1

3

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());
James_D
  • 201,275
  • 16
  • 291
  • 322
  • 2
    Those examples are generally of pretty low quality. For example, the properties should have type `StringProperty`, not `SimpleStringProperty`, and their `get*()` and `set*(...)` methods should be `final`. If I get time I'll file a request to improve them at jira. – James_D May 08 '14 at 16:15
  • Perfect!!! Thanks a lot for helping me out. Also which is the best place to get examples for JavaFX8 components which can act as a reference. – HemantS May 08 '14 at 18:07
  • 1
    You're looking in the right place, and most of the official tutorials are pretty good. They just seemed to drop the ball a bit on the TableView, TreeView, and TreeTableView example source code. – James_D May 08 '14 at 19:13