Java Reference
In-Depth Information
System.out.println("Setting phoneNumber on the JavaBeans property");
try {
person.setPhoneNumber("888-555-1212");
} catch (PropertyVetoException e) {
System.out.println("A JavaBeans property change is vetoed.");
}
System.out.println("person.getPhoneNumber() = " + person.getPhoneNumber());
}
}
In the adaptJavaBeanProperty() method, we instantiated a Person bean and adapted its name JavaBeans
property into a JavaFX JavaBeanStringProperty . To help you understand when a ChangeEvent is delivered to the
nameProperty , we added a ChangeListener (in the form of a lambda expression) to it. Because name is not a bound
property, when we call person.setName() , the nameProperty is not aware of the change. To notify nameProperty of
the change, we call its fireValueChangedEvent() method. When we call nameProperty.get() , we get the name that
we have set on the person bean. Conversely, after we call nameProperty.set() , a call to person.getName() will return
what we have set on nameProperty .
In the adaptBoundProperty() method, we instantiated a Person bean and adapted its address JavaBeans
property into a JavaFX JavaBeanStringProperty . To help you understand when a ChangeEvent is delivered to the
addressProperty , we added a ChangeListener (in the form of a lambda expression) to it. Because address is a
bound property, the addressProperty is registered as a PropertyChangeListener to the person bean; therefore
when we call person.setAddress() , the addressProperty is notified immediately without us having to call the
fireValuechangedEvent() method.
In the adaptConstrainedProperty() method, we instantiated a Person bean and adapted its phoneNumber
JavaBeans property into a JavaBeanStringProperty . Again we added a ChangeListener to it. Because phoneNumber
is a constrained property, phoneNumberProperty is capable of vetoing person.setPhoneNumber() calls. When that
happens, the person.setPhoneNumber() call throws a PropertyVetoException . The phoneNumberProperty will veto
such a change if it is itself bound to another JavaFX property. We call person.setPhoneNumber() twice, once before we
bind phoneNumberProperty to another JavaFX property, and once after phoneNumberProperty is bound. The first call
succeeds in altering the value of the phoneNumberProperty , and the second call throws a PropertyVetoException .
When we run the program in Listing 4-17, the following output is printed to the console:
Setting name on the JavaBeans property
Calling fireValueChange
JavaFX property StringProperty [bean: Person@776ec8df, name: name, value: Weiqi Gao] changed:
oldValue = null, newValue = Weiqi Gao
nameProperty.get() = Weiqi Gao
Setting value on the JavaFX property
JavaFX property StringProperty [bean: Person@776ec8df, name: name, value: Johan Vos] changed:
oldValue = Weiqi Gao, newValue = Johan Vos
person.getName() = Johan Vos
Setting address on the JavaBeans property
JavaFX property StringProperty [bean: Person@41629346, name: address, value: 12345 main Street]
changed:
oldValue = null, newValue = 12345 main Street
Search WWH ::




Custom Search