1

When I bidirectionally bind two properties in JavaFx, which start out with different values, which gets taken?

ObjectProperty<MyType> property1 = new SimpleObjectProperty(value1);
ObjectProperty<MyType> property2 = new SimpleObjectProperty(value2);
property1.bindBidirectional(property2);
// will both properties be set to value1 or to value2?

In my tests (slightly different setup), property2 seems to be set to the value of property1. However, is this a coincidence or is this behavior that I can depend on? I did not find anything about this in the API.

Because I can't be sure, I currently use the following pattern, but this seems redundant to me:

ObjectProperty<MyType> property1 = new SimpleObjectProperty(value1);
ObjectProperty<MyType> property2 = new SimpleObjectProperty(value2);
property1.setValue(property2.getValue());
property1.bindBidirectional(property2);
Thirler
  • 20,239
  • 14
  • 63
  • 92
  • Why not give them the same initial value? – Bohemian Jan 17 '17 at 15:22
  • @Bohemian In realistic situations these are not next to each other. But there is one leading value in my model, where a default is set. Then the controls will bind to this default value (for instance the selected value of a choicebox) – Thirler Jan 17 '17 at 15:39

1 Answers1

0

The javadoc does not specify behaviour, so whatever tests and discoveries you make should not be relied upon, because the next release of Java could change the implementation.

Instead of relying on undocumented behaviour, code so the internal implementation doesn't matter.

For example, give the two properties the same value:

ObjectProperty<MyType> property1 = new SimpleObjectProperty(value1);
ObjectProperty<MyType> property2 = new SimpleObjectProperty(value1);
property1.bindBidirectional(property2);
Bohemian
  • 412,405
  • 93
  • 575
  • 722