Yaerox
Yaerox

Reputation: 678

Java(fx) - TableView - Did I understood the usage of setCellValueFactory and PropertyValueFactory?

From the eclipse help:

setCellValueFactory

The cell value factory needs to be set to specify how to populate all cells within a single TableColumn. A cell value factory is a Callback that provides a CellDataFeatures instance, and expects an ObservableValue to be returned. The returned ObservableValue instance will be observed internally to allow for immediate updates to the value to be reflected on screen.


CellDataFeatures

A support class used in TableColumn as a wrapper class to provide all necessary information for a particular Cell.


PropertyValueFactory

Creates a default PropertyValueFactory to extract the value from a given TableView row item reflectively, using the given property name.


Explained in my words:

myTableViewColumn.setCellValueFactory(new PropertyValueFactory<MyModelClass, String>("name"));

setCellValueFactory specifies how to populate all cells in the column myTableViewColumn. The data-type used here is a property. By using PropertyValueFactory we get the specified property by looking for the property named "name" (declared in MyModelClass private final SimpleStringProperty name = new SimpleStringProperty("");).

Did I understood the usage of setCellValueFactory and PropertyValueFactory?

Upvotes: 0

Views: 1642

Answers (2)

fabian
fabian

Reputation: 82491

I cannot tell if you understood how it's used; The description seems seems to be mostly correct, but there are some details you did not get correct:

  • A Callback itself does not provide a value. The cellValueFactory is a Callback that takes a CellDataFeatures instance as parameter of it's method and returns a ObservableValue; The value stored in the ObservableValue instance is used as item for the TableCell.
    Furthermore the value is only passed to the TableCell responsible for displaying the value. What this cell does with the new value is completely up to it's implementor. A change may or may not be visible on screen and a cell needs not be displayed on screen when the value is changed (It can be scrolled out of view using the TableView's scrollbar).

  • PropertyValueFactory does not look for the field. It looks for a nameProperty() method that is used to retrieve the ObservableValue.
    If there is no such method, it looks for a getter method: getName() or isName(). In the latter case an ObservableValue instance wrapping the value returned by the getter is used (; Note that this makes automatic updates impossible).

Upvotes: 1

ievinm
ievinm

Reputation: 80

Yes that would be correct. Note as well that the data for the table needs to be set with something like the following:

ObservableList<MyModelClass> tableData= FXCollections.observableArrayList();
table.setItems(tableData);

Basically, this table has a list of MyModelClass objects. The cell factory is invoked to bind the desired field from the object to the cells in a given column.

setCellValueFactory can also be used with a lambda rather than a PropertyValueFactory, as seen below.

myTableViewColumn.setCellValueFactory(cellData -> cellData.getValue().name);

Both do the same thing, binding the values from the name field in the list of objects to that column.

Upvotes: 2

Related Questions