Binding Expressions for JavaBean in JavaFX - Java JavaFX

Java examples for JavaFX:Bindable Properties

Description

Binding Expressions for JavaBean in JavaFX

Demo Code

import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class Main {

  public static void main(String[] args) {

    System.out.println("Binding a Contact bean [Bi-directional binding]");
    Employee contact = new Employee("John", "Doe");
    StringProperty fname = new SimpleStringProperty();
    fname.bindBidirectional(contact.firstNameProperty());
    StringProperty lname = new SimpleStringProperty();
    lname.bindBidirectional(contact.lastNameProperty());

    System.out.println("Current - StringProperty values   : "
        + fname.getValue() + " " + lname.getValue());
    System.out.println("Current - Contact values          : "
        + contact.getFirstName() + " " + contact.getLastName());

    System.out.println("Modifying StringProperty values");
    fname.setValue("Jane");
    lname.setValue("Deer");

    System.out.println("After - StringProperty values   : " + fname.getValue()
        + " " + lname.getValue());
    System.out.println("After - Contact values          : "
        + contact.getFirstName() + " " + contact.getLastName());

    System.out.println();//from w w w .j a va2 s  . c om
  }
}

class Employee {

  private SimpleStringProperty firstName = new SimpleStringProperty();
  private SimpleStringProperty lastName = new SimpleStringProperty();

  public Employee(String fn, String ln) {
    firstName.setValue(fn);
    lastName.setValue(ln);
  }

  public final String getFirstName() {
    return firstName.getValue();
  }

  public StringProperty firstNameProperty() {
    return firstName;
  }

  public final void setFirstName(String firstName) {
    this.firstName.setValue(firstName);
  }

  public final String getLastName() {
    return lastName.getValue();
  }

  public StringProperty lastNameProperty() {
    return lastName;
  }

  public final void setLastName(String lastName) {
    this.lastName.setValue(lastName);
  }
}

Related Tutorials