Looking at the JavaDocs for TableColumn
you see this:
firstNameCol.setCellValueFactory(new Callback<CellDataFeatures<Person, String>, ObservableValue<String>>() {
public ObservableValue<String> call(CellDataFeatures<Person, String> p) {
return new ReadOnlyObjectWrapper(p.getValue().getFirstName());
}
});
The idea being to convert a String value into a read only Observable
that can be passed to a cell.
However, looking at the JavaDocs for ReadOnlyObjectWrapper
, you see this:
This class provides a convenient class to define read-only properties. It creates two properties that are synchronized. One property is read-only and can be passed to external users. The other property is read- and writable and should be used internally only.
The class itself extends from SimpleObjectProperty
and only adds one method, getReadOnlyProperty()
, which you can see is not called in the TableColumn
example.
Looking at the TableColumn
source code (JFX 21) you can see this:
public final ObservableValue<T> getCellObservableValue(S var1) {
Callback var2 = this.getCellValueFactory();
if (var2 == null) {
return null;
} else {
TableView var3 = this.getTableView();
if (var3 == null) {
return null;
} else {
CellDataFeatures var4 = new CellDataFeatures(var3, this, var1);
return (ObservableValue)var2.call(var4);
}
}
}
There’s nothing in this code that checks to see if the return value is a ReadOnlyObjectWrapper
(or typed equivalent) and then call getReadOnlyProperty()
. It just (effectively) casts the result to ObservableValue<T>
and then returns it.
Is this just a mistake in the JavaDocs? Or am I missing something important? Because I’m confused now.
More than anything, I’m at a loss to understand the use case for ReadOnlyObjectWrapper
. Any SimpleObjectProperty
can be cast to ReadOnlyObjectProperty
or ObservableValue
just the way that the TableColumn
source code does. When would you use ReadOnlyObjectWrapper.getReadOnlyProperty()
?????