BindingDemo.java (V1)

JavaFX introduces a new concept called binding property that enables a target object to be bound to a source object. If the value in the source object changes, the target property is also changed automatically. The target object is simply called a binding object or a binding property.

Command Window output

C:\ece538\JavaFX\JavaPP\ch14>java BindingDemo
Exception in thread "main" java.lang.RuntimeException: A bound value cannot be set.
        at javafx.beans.property.DoublePropertyBase.set(DoublePropertyBase.java:143)
        at javafx.beans.property.DoubleProperty.setValue(DoubleProperty.java:79)
        at BindingDemo.main(BindingDemo.java:9)

This is what happens if we try to set the value of a bound property.

See BindingDemo.java (working example).


BindingDemo.java

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;

public class BindingDemo {
  public static void main(String[] args) {       
    DoubleProperty d1 = new SimpleDoubleProperty(1);
    DoubleProperty d2 = new SimpleDoubleProperty(2);
    d1.bind(d2);
    d1.setValue(36.2);
    System.out.println("d1 is " + d1.getValue() 
      + " and d2 is " + d2.getValue());
    d2.setValue(70.2);
    System.out.println("d1 is " + d1.getValue() 
      + " and d2 is " + d2.getValue());
  }
}


Maintained by John Loomis, updated Sat Feb 10 16:31:36 2018