(Following this question, this is what prompted)
I have a model class with LongProperty :
public class Model { private final SimpleLongProperty number = new SimpleLongProperty(this, "number"); public long getNumber() { return number.get(); } public void setNumber(long number) { this.number.set(number); } public LongProperty numberProperty() { return number; } }
Now in my controller there is a TableColumn<Model, Long> colNumber , which I want to associate with this property. I know that I can use PropertyValueFactory , but I do not like the idea of ββassigning a property by name when I can pass it programmatically, and I have a / ide compiler check. Basically I want to do something like this (I really want to make it more concise, for example, at the end):
colNumber.setCellValueFactory( cdf -> cdf.getValue().numberProperty() );
but this gives me a compilation error:
java: incompatible types: invalid return type in lambda expression javafx.beans.property.ObjectProperty cannot be converted to javafx.beans.value.ObservableValue
As I said, I know that I can use PropertyValueFactory and also have static final strings for property names, but I find it less elegant. Is there any way to make this software approach work? Some kind of magic?
Application:
The actual way I wanted to do this is to use a helper method:
private <S,T> Callback<CellDataFeatures<S,T>, ObservableValue<T>> propertyFactory(Callback<S, ObservableValue<T>> callback) { return cdf-> callback.call(cdf.getValue()); }
and then I can just use
colNumber.setCellValueFactory(propertyFactory(Model::numberProperty));
which keeps my code very concise and readable, and the compiler checks for typos, etc.