Binding Properties
By using a combination of events and adapters, we can connect beans in many interesting ways. We can even “bind” two beans together so that if a property changes in the first bean, the corresponding property is automatically changed in the second bean. In this scenario, the beans don’t necessarily have to be of the same type, but in order to make sense, the properties do.
Close the Molecule file and start
a new one. Grab two NumericField beans from
the palette, drop them in the workspace, and select one of them. You’ll
probably want to set the AbsoluteLayout again. You can also adjust the
width of the fields by dragging them at the sides. You’ll notice that a
NumericField has many of the standard
properties of a Swing component. If you look in the Other Properties
section of the Properties pane, you can find an integer property called
value that represents the numeric value
of the field. You can set it there or enter a number directly into the
field when you run the program. NumericField rejects nonnumeric text.
Let’s bind the value property of
one of the fields to the other. Activate the Connection Wizard to create a
connection between the two fields. Click first on numericField1 and then on numericField2 so that numericField1 is the source. In the wizard,
choose the propertyChange() event of
the source field. This is the listener method for PropertyChangeEvent, a generic event sent by beans when one of their properties changes. When a bean fires property change ...