Callback<P, R>
where P and R are Java class types. The "P" parameter is the type of data passed "into" the
"call()" method. The "R" parameter is the data type to be returned "from" the "call()"
method.
Returning now to the setCellValueFactory(), we now understand that it wants a Callback
object as a parameter and we now see that a Callback object needs to know its "call()" input and
output data types. So what should those be for a setCellValueFactory()? The answer is
that the input to call will be an instance of "TableColumn.CellDataFeatures" and the
return will be an "ObservableValue".
Yikes!! Even more objects!!
First, the TableColumn.CellDataFeatures. This object is declared with the format:
TableColumn.CellDataFeatures<S,T>
where "S" and "T" are Java class types.
The "S" is the type of object contained in the list of items shown in the table. So if the table is used
to show a list of objects of type "Person", then "S" will be "Person".
The "T" is the type of the object that this specific column will display. So if the current column
were showing people's names, the "T" type might be a "String".
From a TableColumn.CellDataFeatures() object, we can get three important pieces of
information:
• The reference to the TableView that contains the column
• The reference to the TableColumn that is to be visualized
• The reference to the value of the current cell
So, bringing it together again, the TableColumn.CellDataFeatures provides the table,
table column and value of a specific cell. Since this is passed into the Callback associated with
the setCellValueFactory(), we now have an inkling of the relationship. This
TableColumn.CellDataFeatures owns the data to be shown in a specific cell!!
Rather than simply just return the data for the cell (eg. a string) from the Callback's call()
method, what we do is return an ObservableValue. This allows us to update the value of the
cell's data and the cell will "automatically" update itself.
Here is an example of putting it all together:
someCloumn.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<SearchResult, String>,
ObservableValue<String>>() {
@Override
public ObservableValue<String> call(CellDataFeatures<SearchResult, String> aCellDataFeatures) {
return new SimpleStringProperty(aCellDataFeatures.getValue().getTaskId());
}
});
The recipe above pre-dates the arrival of Java 8 with its Lambda capabilities. This can now be
more succinctly coded as:
someCloumn.setCellValueFactory(aCellDataFeatures -> {
return new SimpleStringProperty(aCellDataFeatures.getValue().getTaskId());
});
One more consideration relating to the setCellValueFactory() is the utility class called
PropertyValueFactory(). This is a really cool class. Its constructor takes the name of a
Page 272
Kommentare zu diesen Handbüchern