JavaFX:双向绑定(bind)的初始值

标签 javafx data-binding javafx-bindings

当我对这两个属性进行绑定(bind)时会发生什么?

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

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

Bindings.bindBidirectional(propertyA, propertyB);

如果两个属性都应该持有相同的对象引用,那么在这个绑定(bind)之后,这两个属性会持有 ObjectA 的引用吗?或 ObjectB ?

最佳答案

你打电话时:

Bindings.bindBidirectional(propertyA, propertyB);
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 {
    }
}

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

Binding implementation source

Note the call 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;
}

其他问题的答案

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



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

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

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

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



是的,该方法的源代码如下:
list1.setAll(list2);

关于JavaFX:双向绑定(bind)的初始值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41014304/

相关文章:

java - Thread.sleep() 的意外行为

java - 可以使用 CSS 将标记扩展到 JavaFX 中的复选框之外吗?

java - 一个程序(eclipse Mars)需要java 8,一个程序(控制台应用程序)需要java 7 怎么办?

java - 无法将 java.lang.String 类型的属性值转换为所需的 java.util.Date 类型

java - 在JavaFX中从mysql数据库添加动态数据时回调有什么用

WPF - Listview 数据绑定(bind)列宽问题

c# - 从 MainPage.xaml.cs 文件获取字符串变量的值到 Windows Phone 中的 MainPage.xaml 文件

java - 如何将值绑定(bind)到 ObservableList 的大小?

JavaFX 复杂字符串绑定(bind)

JavaFX:绑定(bind)到插图