Defining a Property and attach change listener : ChangeListener « JavaFX « Java






Defining a Property and attach change listener

 

import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class Main {

  public static void main(String[] args) {

    MyClass myObject = new MyClass();

    myObject.amountDueProperty().addListener(new ChangeListener() {
      @Override
      public void changed(ObservableValue o, Object oldVal, Object newVal) {
        System.out.println("changed!");
      }
    });

    myObject.setAmountDue(100.00);

  }
}

class MyClass {

  // Define a variable to store the property
  private DoubleProperty amountDue = new SimpleDoubleProperty();

  // Define a getter for the property's value
  public final double getAmountDue() {
    return amountDue.get();
  }

  // Define a setter for the property's value
  public final void setAmountDue(double value) {
    amountDue.set(value);
  }

  // Define a getter for the property itself
  public DoubleProperty amountDueProperty() {
    return amountDue;
  }

}

   
  








Related examples in the same category

1.Adding change listener to selected property