以编程方式将LongProperty应用于TableColumn(与语义相对) [英] Apply LongProperty to TableColumn programmatically (vs semantically)

查看:130
本文介绍了以编程方式将LongProperty应用于TableColumn(与语义相对)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(关注这个问题,这就是提示它)

(Following this question, this is what prompted it)

我的模型类有一个 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; }
}

现在,在我的控制器中我有一个 TableColumn< ;模型,长> colNumber 我要绑定到此属性。我知道我可以使用 PropertyValueFactory ,但是当我可以通过编程方式传递它并且使用编译器/ ide拼写检查时,我不喜欢通过名称给出属性的想法我。基本上我想做这样的事情(我实际上想让它更简洁,最后的例子):

Now, in my controller I have a TableColumn<Model, Long> colNumber which I want to bind to this property. I know I can use PropertyValueFactory, but I don't like the idea of giving the property by name when I can pass it programmatically, and have the compiler/ide spell-check me. Basically I want to do something like this (I actually want to make it more concise, example in the end):

colNumber.setCellValueFactory( cdf -> cdf.getValue().numberProperty() );

但是这给了我一个编译错误:

but this gives me a compilation error:

java:不兼容类型:lambda表达式中的错误返回类型
javafx.beans.property.ObjectProperty无法转换为javafx.beans.value.ObservableValue

java: incompatible types: bad return type in lambda expression javafx.beans.property.ObjectProperty cannot be converted to javafx.beans.value.ObservableValue

正如我所说,我知道我可以使用 PropertyValueFactory ,并且还有属性名称的静态最终字符串,但我发现它不那么优雅。有没有办法让这种程序化方法有效?一些铸造魔法?

As I said, I know I can use PropertyValueFactory, and also have static final strings for the property names, but I find it less elegant. Is there a way to make this programmatic approach work? Some casting-magic?

附录:

我想要的实际方法是使用辅助方法:

Appendix:
The actual way I wanted to make it is with a helper method:

private <S,T> Callback<CellDataFeatures<S,T>, ObservableValue<T>> propertyFactory(Callback<S, ObservableValue<T>> callback) {
    return cdf-> callback.call(cdf.getValue());
}

然后我可以使用

colNumber.setCellValueFactory(propertyFactory(Model::numberProperty));

这使我的代码非常简洁和可读,并让编译器检查我是否有错别字等。

which keeps my code very concise and readable, and has the compiler checking me for typos etc.

推荐答案

你可以做到

colNumber.setCellValueFactory( cdf -> cdf.getValue().numberProperty().asObject() );

我认为(我需要测试,但这似乎是正确的)你也可以利用在模型中自动装箱和取消装箱,并将该属性实现为 ObjectProperty< Long>

I think (I'd need to test, but this seems right) you could also take advantage of autoboxing and unboxing in your model, and implement the property as an 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; }
}

这种方法的一个缺点是它不允许任何算术绑定,例如你不能做 someValue.bind(model.numberProperty()。multiply(2)); 等等(另一个是你可以调用模型.nu​​mberProperty()。set(null); 意外,并造成各种破坏。)

One disadvantage to this approach is it would not allow any of the arithmetic bindings, e.g. you couldn't do someValue.bind(model.numberProperty().multiply(2)); etc. (Another is that you could call model.numberProperty().set(null); by accident, and wreak all kinds of havoc.)

另一种解决方案,当然,是使您的表格列为 TableColumn< Model,Number> ,但可能还有其他原因不这样做。

One other solution, of course, is to make your table column a TableColumn<Model, Number>, but there may be other reasons not to do that.

FWIW,我肯定会主张避免 PropertyValueFactory 。这是JavaFX 2.0中引入的一个便利类(即在我们有lambda之前),当实现允许编译器检查的回调非常冗长时。现在它基本上是多余的(并且应该被弃用,imho,或者至少应该实现你所概述的实用方法)。

FWIW, I would definitely advocate avoiding PropertyValueFactory. That was a convenience class introduced in JavaFX 2.0 (i.e. before we had lambdas), when it was very verbose to implement the callback that allowed for compiler checking. Now it is basically redundant (and should be deprecated, imho, or should at least implement a utility method of the kind you outline).

这篇关于以编程方式将LongProperty应用于TableColumn(与语义相对)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆