Java Reference
In-Depth Information
bidirectionally. Once they enter this relationship, calling the set() or setValue() methods on either Property object
will cause both objects' values to be updated. Calling unbindBidirectional() releases any existing bidirectional
binding the caller and the argument may have. The program in Listing 4-2 shows a simple bidirectional binding
at work.
Listing 4-2. BidirectionalBindingExample.java
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class BidirectionalBindingExample {
public static void main(String[] args) {
System.out.println("Constructing two StringProperty objects.");
StringProperty prop1 = new SimpleStringProperty("");
StringProperty prop2 = new SimpleStringProperty("");
System.out.println("Calling bindBidirectional.");
prop2.bindBidirectional(prop1);
System.out.println("prop1.isBound() = " + prop1.isBound());
System.out.println("prop2.isBound() = " + prop2.isBound());
System.out.println("Calling prop1.set(\"prop1 says: Hi!\")");
prop1.set("prop1 says: Hi!");
System.out.println("prop2.get() returned:");
System.out.println(prop2.get());
System.out.println("Calling prop2.set(prop2.get() + \"\\nprop2 says: Bye!\")");
prop2.set(prop2.get() + "\nprop2 says: Bye!");
System.out.println("prop1.get() returned:");
System.out.println(prop1.get());
}
}
In this example we created two SimpleStringProperty objects called prop1 and prop2 , created a bidirectional
binding between them, and then called set() and get() on both properties.
When we run the program in Listing 4-2, the following output is printed to the console:
Constructing two StringProperty objects.
Calling bindBidirectional.
prop1.isBound() = false
prop2.isBound() = false
Calling prop1.set("prop1 says: Hi!")
prop2.get() returned:
prop1 says: Hi!
Calling prop2.set(prop2.get() + "\nprop2 says: Bye!")
prop1.get() returned:
prop1 says: Hi!
prop2 says: Bye!
Search WWH ::




Custom Search