Java Reference
In-Depth Information
The getNewValue() method is used to determine what the new value of
the accountNumber property is being requested.
■■
If the newValue is not between 1 and 100, the property change is
vetoed. Otherwise, the listener does nothing.
■■
Hooking up a constrained property is similar to hooking up a bound prop-
erty in the Bean Builder, except that the target method for the listener is
vetoableChange(). If you are interested in seeing constrained properties in
action, load the customer_bean2.jar file. (To download this file, go to this
book's Web site.) Hook up the Customer bean to the StoreOwner bean, regis-
tering the StoreOwner as a listener to the accountNumber property.
Overview of Events
JavaBeans communicate with each other through events. You have seen events
already with the bound and constrained properties, except that you used util-
ity classes such as PropertyChangeSupport to register and notify listeners. In
this section, I will show you how to write a bean that generates an event and
how to register another bean to listen to that event.
During the introspection process, a builder tool looks for methods of the fol-
lowing format:
public void add<event_name>Listener(<event_name>Listener x)
public void remove<event_name>Listener(<event_name>Listener x)
public <event_name>Listener [] get<event_name>Listeners()
These three methods are used to denote that a bean is the source of an event
named <event_name>. The <event_name>Listener interface contains the
methods that the source of the event invokes and must match the name used
in the add and remove methods.
For example, if you want a bean to be the source of a java.awt.ActionEvent,
the following three methods need to appear in the bean class:
public void addActionListener(ActionListener x)
public void removeActionListener(ActionListener x)
public ActionListener [] getActionListeners()
A bean can be the source of any event object that extends java.util.EventOb-
ject and whose listener interface extends java.util.EventListener. Thus, all the
Swing and AWT events can be used in JavaBeans, as well as any events that
you define.
Search WWH ::




Custom Search