Createn conditional binding with When : binding « JavaFX « Java






Createn conditional binding with When

 

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

public class Main {
    public static void main(String[] args) {
        DoubleProperty a = new SimpleDoubleProperty(0);
        DoubleProperty b = new SimpleDoubleProperty(0);

        DoubleBinding s = a.add(b).divide(2.0D);

        final DoubleBinding aBinding = new When(a.add(b).greaterThan(b)
                .and(a.add(a).greaterThan(b)))
                .then(s.multiply(s.subtract(a))
                        .multiply(s.subtract(b)))
                .otherwise(0.0D);

        a.set(3);
        b.set(4);
        System.out.println(a.get());
        System.out.println(b.get());
        System.out.println(aBinding.get());

        a.set(2);
        b.set(2);
        System.out.println(a.get());
        System.out.println(b.get());
        System.out.println(aBinding.get());

    }
}

   
  








Related examples in the same category

1.Create custom binding by subclassing DoubleBinding
2.Bind and unbind
3.Bidirectional Binding Example
4.Binding Group translateY Property