JavaFX:双向绑定的初始值

Jai*_*Jai 5 data-binding javafx javafx-bindings

当我绑定这两个属性时会发生什么?

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

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

Bindings.bindBidirectional(propertyA, propertyB);
Run Code Online (Sandbox Code Playgroud)

如果两个属性都应该持有相同的对象引用,则在此绑定之后,两个属性都将持有ObjectAObjectB吗?

jew*_*sea 6

你打电话的时候:

Bindings.bindBidirectional(propertyA, propertyB);
Run Code Online (Sandbox Code Playgroud)

的值propertyA将设置为的值propertyB

因此,在这种情况下,正如propertyB已经提到的ObjectB,在调用之后,两个属性都将引用: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 {
    }
}
Run Code Online (Sandbox Code Playgroud)

测试输出

propertyA = ObjectProperty [值:appCC.xyzzy.HellBound$ObjectB@7c3df479]
propertyB = ObjectProperty [值: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;
}
Run Code Online (Sandbox Code Playgroud)

回答其他问题

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

因为javadoc是由人类而非神灵编写的。有时,人类会做出深不可测的遗漏。也许神也这样做:-)

我同意应该在Javadoc中提供有用的信息。

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

我假设这对于ObservableLists也应该是相同的,以便Bindings.bindContentBidirectional()应该以相同的方式工作?

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

list1.setAll(list2);
Run Code Online (Sandbox Code Playgroud)