JavaFX:双向绑定的初始值 [英] JavaFX: Initial value of bidirectional binding

查看:826
本文介绍了JavaFX:双向绑定的初始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



What happens when I do binding for these two properties?

ObjectProperty<Object> propertyA = new SimpleObjectProperty<>();
ObjectProperty<Object> propertyB = new SimpleObjectProperty<>();

propertyA.set(new ObjectA());
propertyB.set(new ObjectB());

Bindings.bindBidirectional(propertyA, propertyB);

如果两个属性都保持相同的对象引用,则在此绑定之后,两个属性是持有 ObjectA ObjectB 的引用

If both properties are supposed to hold the same object reference, then after this binding, would both properties be holding the reference of ObjectA or ObjectB?

推荐答案

当您致电:

Bindings.bindBidirectional(propertyA, propertyB);

propertyA 的值将设置为 propertyB

所以在这种情况下,作为 propertyB 已经引用 ObjectB ,调用后,这两个属性都将引用: ObjectB

So in this case, as propertyB already refers to ObjectB, after the call, both properties will refer to: ObjectB

测试代码

import javafx.beans.binding.Bindings;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;

public class HellBound {
    public static void main(String[] args) {
        ObjectProperty<Object> propertyA = new SimpleObjectProperty<>();
        ObjectProperty<Object> propertyB = new SimpleObjectProperty<>();

        propertyA.set(new ObjectA());
        propertyB.set(new ObjectB());

        Bindings.bindBidirectional(propertyA, propertyB);

        System.out.println("propertyA = " + propertyA);
        System.out.println("propertyB = " + propertyB);
    }

    private static class ObjectA {
    }

    private static class ObjectB {
    }
}

测试输出


propertyA = ObjectProperty [value: appCC.xyzzy.HellBound$ObjectB@7c3df479]
propertyB = ObjectProperty [value: appCC.xyzzy.HellBound$ObjectB@7c3df479]

绑定实现源

请注意调用 property1。 setValue(property2.getValue());

public static <T> BidirectionalBinding bind(Property<T> property1, Property<T> property2) {
    checkParameters(property1, property2);
    final BidirectionalBinding binding =
            ((property1 instanceof DoubleProperty) && (property2 instanceof DoubleProperty)) ?
                    new BidirectionalDoubleBinding((DoubleProperty) property1, (DoubleProperty) property2)
            : ((property1 instanceof FloatProperty) && (property2 instanceof FloatProperty)) ?
                    new BidirectionalFloatBinding((FloatProperty) property1, (FloatProperty) property2)
            : ((property1 instanceof IntegerProperty) && (property2 instanceof IntegerProperty)) ?
                    new BidirectionalIntegerBinding((IntegerProperty) property1, (IntegerProperty) property2)
            : ((property1 instanceof LongProperty) && (property2 instanceof LongProperty)) ?
                    new BidirectionalLongBinding((LongProperty) property1, (LongProperty) property2)
            : ((property1 instanceof BooleanProperty) && (property2 instanceof BooleanProperty)) ?
                    new BidirectionalBooleanBinding((BooleanProperty) property1, (BooleanProperty) property2)
            : new TypedGenericBidirectionalBinding<T>(property1, property2);
    property1.setValue(property2.getValue());
    property1.addListener(binding);
    property2.addListener(binding);
    return binding;
}

其他问题的答案


我只是想知道为什么javadoc不会告诉我们这种有用的信息。

I'm just wondering why the javadoc doesn't tell us this kind of useful information.

因为javadoc是由人而不是神写的。有时候人类会造成不可思议的遗漏。也许神也会这样做: - )

Because the javadoc is written by humans and not gods. Sometimes humans make unfathomable omissions. Perhaps gods do too :-)

我同意这是Javadoc中的有用信息。

I agree that it is useful info that should be in the Javadoc.

可以提交错误报告来改进文档( http://bugreport.java.com )。或者 openjfx-dev 开发人员列表的帖子可能会获得开发者具有提交权限来改进它。您可以自己提交补丁,但对大多数人来说,除非他们已经是JDK提交者,他已经签署了 OCA ,这可能不值得。

A bug report could be filed to improve the doc (http://bugreport.java.com). Or a post to the openjfx-dev developer list might get a developer with commit privileges to improve it. You could submit a patch yourself, but, for most people, unless they are already a JDK committer who has signed the OCA, it's probably not worth it.


我认为ObservableLists应该是一样的,这样Bindings.bindContentBidirectional()应该以同样的方式工作? / p>

I'm assuming this should also be the same for ObservableLists, such that Bindings.bindContentBidirectional() should work in the same way?

是的,该方法的源代码如下:

Yeah, the source for that method has the following code:

list1.setAll(list2);

这篇关于JavaFX:双向绑定的初始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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