java - Apply LongProperty to TableColumn programmatically (vs semantically) -
(following this question, prompted it)
i have model class 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 controller have tablecolumn<model, long> colnumber want bind property. know can use propertyvaluefactory, don't idea of giving property name when can pass programmatically, , have compiler/ide spell-check me. want (i want make more concise, example in end):
colnumber.setcellvaluefactory( cdf -> cdf.getvalue().numberproperty() ); but gives me compilation error:
java: incompatible types: bad return type in lambda expression javafx.beans.property.objectproperty cannot converted javafx.beans.value.observablevalue
as said, know can use propertyvaluefactory, , have static final strings property names, find less elegant. there way make programmatic approach work? casting-magic?
appendix:
actual way wanted make helper method:
private <s,t> callback<celldatafeatures<s,t>, observablevalue<t>> propertyfactory(callback<s, observablevalue<t>> callback) { return cdf-> callback.call(cdf.getvalue()); } and can use
colnumber.setcellvaluefactory(propertyfactory(model::numberproperty)); which keeps code concise , readable, , has compiler checking me typos etc.
you can do
colnumber.setcellvaluefactory( cdf -> cdf.getvalue().numberproperty().asobject() ); i think (i'd need test, seems right) take advantage of autoboxing , unboxing in model, , implement property objectproperty<long>:
public class model { private final objectproperty<long> number = new simpleobjectproperty<>(this, "number", 0l); public long getnumber() { return number.get(); } public void setnumber(long number) { this.number.set(number); } public objectproperty<long> numberproperty() { return number; } } one disadvantage approach not allow of arithmetic bindings, e.g. couldn't somevalue.bind(model.numberproperty().multiply(2)); etc. (another call model.numberproperty().set(null); accident, , wreak kinds of havoc.)
one other solution, of course, make table column tablecolumn<model, number>, there may other reasons not that.
fwiw, advocate avoiding propertyvaluefactory. convenience class introduced in javafx 2.0 (i.e. before had lambdas), when verbose implement callback allowed compiler checking. redundant (and should deprecated, imho, or should @ least implement utility method of kind outline).
Comments
Post a Comment