Binding InvalidationListener : InvalidationListener « JavaFX « Java






Binding InvalidationListener

 
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.beans.binding.Bindings;
import javafx.beans.binding.NumberBinding;
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;

class MyClass {

  private DoubleProperty amountDue = new SimpleDoubleProperty();

  public final double getAmountDue() {
    return amountDue.get();
  }

  public final void setAmountDue(double value) {
    amountDue.set(value);
  }

  public DoubleProperty amountDueProperty() {
    return amountDue;
  }

}

public class Main {

  public static void main(String[] args) {

    MyClass myObject1 = new MyClass();
    MyClass myObject2 = new MyClass();
    MyClass myObject3 = new MyClass();

    NumberBinding total = Bindings.add(
        myObject1.amountDueProperty().add(myObject2.amountDueProperty()),
        myObject3.amountDueProperty());
    total.addListener(new InvalidationListener() {
      @Override
      public void invalidated(Observable o) {
        System.out.println("The binding is now invalid.");
      }
    });
    myObject1.setAmountDue(200.00);
    myObject2.setAmountDue(100.00);
    myObject3.setAmountDue(75.00);
    System.out.println(total.getValue());
    myObject3.setAmountDue(150.00);
    System.out.println(total.getValue());
  }
}

   
  








Related examples in the same category