JavaFX:双向绑定的初始值
JavaFX: Initial value of bidirectional binding
当我对这两个属性进行绑定时会发生什么?
ObjectProperty<Object> propertyA = new SimpleObjectProperty<>();
ObjectProperty<Object> propertyB = new SimpleObjectProperty<>();
propertyA.set(new ObjectA());
propertyB.set(new ObjectB());
Bindings.bindBidirectional(propertyA, propertyB);
如果两个属性应该持有相同的对象引用,那么在这个绑定之后,两个属性是否都持有 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]
绑定实现源码
注意调用 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). 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,这可能不值得。
I'm assuming this should also be the same for ObservableLists, such that Bindings.bindContentBidirectional() should work in the same way?
是的,该方法的源代码如下:
list1.setAll(list2);
当我对这两个属性进行绑定时会发生什么?
ObjectProperty<Object> propertyA = new SimpleObjectProperty<>();
ObjectProperty<Object> propertyB = new SimpleObjectProperty<>();
propertyA.set(new ObjectA());
propertyB.set(new ObjectB());
Bindings.bindBidirectional(propertyA, propertyB);
如果两个属性应该持有相同的对象引用,那么在这个绑定之后,两个属性是否都持有 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]
绑定实现源码
注意调用 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). 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,这可能不值得。
I'm assuming this should also be the same for ObservableLists, such that Bindings.bindContentBidirectional() should work in the same way?
是的,该方法的源代码如下:
list1.setAll(list2);