Binding a Property to an Expression - Java JavaFX

Java examples for JavaFX:Bindable Properties

Description

Binding a Property to an Expression

Demo Code

import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;

public class Main {
  public static void main(String[] args) {
    IntegerProperty x = new SimpleIntegerProperty(10);
    IntegerProperty y = new SimpleIntegerProperty(20);
    IntegerProperty z = new SimpleIntegerProperty(60);

    z.bind(x.add(y));//from ww  w .j  av  a 2  s  . c  om

    System.out.println("After binding z: Bound = " + z.isBound() + ", z = " + z.get());

    x.set(15);
    y.set(19);
    System.out.println("After changing x and y: Bound = " + z.isBound() + ", z = " + z.get());

    z.unbind();

    x.set(100);
    y.set(200);
    System.out.println("After unbinding z: Bound = " + z.isBound() + ", z = " + z.get());
  }
}

Related Tutorials