gpt4 book ai didi

JavaFX:绑定(bind)和弱监听器

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:49:16 24 4
gpt4 key购买 nike

来自 Javadoc for bind() :

Note that JavaFX has all the bind calls implemented through weak listeners. This means the bound property can be garbage collected and stopped from being updated.

现在考虑我有两个属性 ObjectProperty<Foo> shortLived居住在ShortLivedObjectObjectProperty<Foo> longLived居住在LongLivedObject .

我像这样绑定(bind)它们:

longLivedObject.longLivedProperty().bind(shortLivedObject.shortLivedProperty());

因为绑定(bind)使用弱监听器,所以如果shortLivedObject是否收集了垃圾,shortLived属性(property)也将被垃圾收集。那么,这是否意味着 longLived属性仍然绑定(bind),但永远不会更新?那会留下吗longLived处于绑定(bind)状态的属性(使进一步绑定(bind)变得不可能),但什么都不做?

最佳答案

So, does that means that longLived property is still bound, but it will never be updated?

假设 shortLivedProperty 已被垃圾回收,shortLivedProperty 将永远不会再次失效。因此,longLived 的监听器将永远不会被再次调用和更新。

Does that leave longLived property in a bound state (making further binding impossible), but does nothing?

无论绑定(bind)状态如何,您都应该始终能够将属性绑定(bind) observable,因为旧的 observable 属性将为您删除/解除绑定(bind):

public void bind(final ObservableValue<? extends T> newObservable) {
if (newObservable == null) {
throw new NullPointerException("Cannot bind to null");
}

if (!newObservable.equals(this.observable)) {
unbind();
observable = newObservable;
if (listener == null) {
listener = new Listener(this);
}
observable.addListener(listener);
markInvalid();
}
}

关于JavaFX:绑定(bind)和弱监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46679565/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com