Create custom binding by subclassing DoubleBinding : binding « JavaFX « Java






Create custom binding by subclassing DoubleBinding

 


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

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() {
            private double value;

            {
                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());
    }
}

   
  








Related examples in the same category

1.Bind and unbind
2.Createn conditional binding with When
3.Bidirectional Binding Example
4.Binding Group translateY Property