JavaFX How to - Extend DoubleProperty to create custom bindings








Question

We would like to know how to extend DoubleProperty to create custom bindings.

Answer

import javafx.beans.binding.DoubleBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
//w  ww.  j  ava2  s .c om
public class Main {
  public static void main(String[] args) {
    final DoubleProperty x = new SimpleDoubleProperty(null, "x", 2.0);
    final DoubleProperty y = new SimpleDoubleProperty(null, "y", 3.0);
    DoubleBinding area = new DoubleBinding() {
      {
        super.bind(x, y);
      }
      @Override
      protected double computeValue() {
        System.out.println("computeValue() is called.");
        return x.get() * y.get();
      }
    };
    System.out.println("area.get() = " + area.get());
    x.set(5);
    y.set(7);
    System.out.println("area.get() = " + area.get());
  }
}

The code above generates the following result.