These methods are used to add or remove a listener for the specified event. The version of
AddTListener( ) that does not throw an exception can be used to multicast an event, which
means that more than one listener can register for the event notification. The version that
throws TooManyListenersException unicasts the event, which means that the number of
listeners is restricted to one. In either case, removeTListener( ) is used to remove the listener.
For example, assuming an event interface type called TemperatureListener, a Bean that monitors
temperature might supply the following methods:
public void addTemperatureListener(TemperatureListener tl) {
public void removeTemperatureListener(TemperatureListener tl) {
Methods and Design Patterns
Design patterns are not used for naming nonproperty methods. The introspection mechanism
finds all of the public methods of a Bean. Protected and private methods are not presented.
Using the BeanInfo Interface
As the preceding discussion shows, design patterns implicitly determine what information is
available to the user of a Bean. The BeanInfo interface enables you to explicitly control what
information is available. The BeanInfo interface defines several methods, including these:
PropertyDescriptor[ ] getPropertyDescriptors( )
EventSetDescriptor[ ] getEventSetDescriptors( )
MethodDescriptor[ ] getMethodDescriptors( )
They return arrays of objects that provide information about the properties, events, and methods
of a Bean. The classes PropertyDescriptor, EventSetDescriptor, and MethodDescriptor
are defined within the java.beans package, and they describe the indicated elements. By
implementing these methods, a developer can designate exactly what is presented to a user,
bypassing introspection based on design patterns.
When creating a class that implements BeanInfo, you must call that class bnameBeanInfo,
where bname is the name of the Bean. For example, if the Bean is called MyBean, then the
information class must be called MyBeanBeanInfo.
To simplify the use of BeanInfo, JavaBeans supplies the SimpleBeanInfo class. It provides
default implementations of the BeanInfo interface, including the three methods just shown.
You can extend this class and override one or more of the methods to explicitly control what
aspects of a Bean are exposed. If you don't override a method, then design-pattern introspection
will be used. For example, if you don't override getPropertyDescriptors( ), then design
patterns are used to discover a Bean's properties. You will see SimpleBeanInfo in action later
in this chapter.
Bound and Constrained Properties
A Bean that has a bound property generates an event when the property is changed. The
event is of type PropertyChangeEvent and is sent to objects that previously registered an
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks