Binding other controls and properties
Text controls aren't the only bindable SWT widgets. All of the standard SWT widgets, such as combos and labels, are available for binding. You can also bind to nonvisual widget properties, such as enabled. Copy the code from Listing 12 into the Person bean.
Listing 12. Adding enabled support to the Person bean
private boolean firstEnabled = true;
public boolean getFirstEnabled() {
return firstEnabled;
}
public void setFirstEnabled(boolean firstEnabled) {
Object oldVal = this.firstEnabled;
this.firstEnabled = firstEnabled;
firePropertyChange("firstEnabled", \
oldVal, this.firstEnabled);
}
|
Now modify the updatePerson() method in the example.
Listing 13. Modifying the updatePerson() method
private void updatePerson() {
person.setFirst("James");
person.setLast("Gosling");
person.setFirstEnabled(false);
}
|
Finally, add the bindings shown below to the end of the createControls() method.
Listing 14. Binding the labels and enablement
ctx.bind(new Property(firstText, "enabled"),
new Property(this.person, "firstEnabled"),
new BindSpec());
ctx.bind(labelFirst,
new Property(this.person, "first"),
new BindSpec());
ctx.bind(labelLast,
new Property(this.person, "last"),
new BindSpec());
|
The new bindings result in the labels of the example changing to the same value as the text widget. The widget for the first field also becomes disabled when you click Change Name. Run the example again and test this functionality.
Another interesting effect of these additional bindings can be demonstrated by typing a few characters in the Last field and pressing Tab. Notice that the Last label also changes. JFace data binding synchronizes the value of the Person bean's last-name field with the widget on focus lost. Because the label is bound to this property, it's also updated.


