Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8118559

synchronize property values of multiple objects with (bidirectional) binding (binding order, property initialisation)

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P4 P4
    • fx2.1
    • fx2.0.1
    • javafx
    • windows 7, jdk 7, javafx 2.0.1

      I'd like to synchronize a value among multiple objects with bidirectional bindings. Example:
      {code}
      import javafx.beans.property.DoubleProperty;
      import javafx.beans.property.SimpleDoubleProperty;
       
      public class SandboxBindingDoubleProperties
      {
      public static void main(String[] args)
      {
      DoubleProperty doubleProperty1 = new SimpleDoubleProperty(1);
      DoubleProperty doubleProperty2 = new SimpleDoubleProperty(2);
      DoubleProperty doubleProperty3 = new SimpleDoubleProperty(3);

      doubleProperty1.bindBidirectional(doubleProperty2);
      doubleProperty2.bindBidirectional(doubleProperty3);
       
      doubleProperty1.set(4);
      System.out.println(doubleProperty1.doubleValue());
      System.out.println(doubleProperty2.doubleValue());
      System.out.println(doubleProperty3.doubleValue());
      doubleProperty2.set(5);
      System.out.println(doubleProperty1.doubleValue());
      System.out.println(doubleProperty2.doubleValue());
      System.out.println(doubleProperty3.doubleValue());
      doubleProperty3.set(6);
      System.out.println(doubleProperty1.doubleValue());
      System.out.println(doubleProperty2.doubleValue());
      System.out.println(doubleProperty3.doubleValue());
      }
      }
      {code}
      The output is:

      4.0
      3.0 <- error, should be 4.0
      3.0 <- error, should be 4.0
      5.0
      5.0
      5.0
      6.0
      6.0
      6.0

      A change in the binding order leads to the expected behaviour:
      {code}
      doubleProperty2.bindBidirectional(doubleProperty3);
      doubleProperty1.bindBidirectional(doubleProperty2);
      {code}
      4.0
      4.0
      4.0
      5.0
      5.0
      5.0
      6.0
      6.0
      6.0

      An additional but superfluous binding to the original example also leads to the expected behaviour:
      {code}
      doubleProperty1.bindBidirectional(doubleProperty2);
      doubleProperty2.bindBidirectional(doubleProperty3);
      doubleProperty3.bindBidirectional(doubleProperty1);
      {code}
      Starting with no-args constructors also leads to expected behaviour:
      {code}
      DoubleProperty doubleProperty1 = new SimpleDoubleProperty();
      DoubleProperty doubleProperty2 = new SimpleDoubleProperty();
      DoubleProperty doubleProperty3 = new SimpleDoubleProperty();
      {code}

            mheinrichs Michael Heinrichs (Inactive)
            duke J. Duke
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: