Advanced Topic The Java Beans Event Model in Detail

The Java 1.1 event model was introduced so that you would no longer have to subclass a GUI component in order to handle events, as you did ^iHi the Java 1.0 version. JFC, AWT, and JavaBeans use the version 1.1 event model, which defines the following:

• Listener— a class that listed for an evert, thaw isg the event haudler

• Event— information abort the event

In this model, to define an event you have to subclass a class from EventObj ect, which contains event information. One piece of information is the source property, which provides the event's origin. In our last example, we had a JButton inrtance called b, which was the source of tide ActionEvent insSanca passed to the event method handlea (in the last example this method was called hello()).

An event listener registers itself with the event source, which notifies the listener when the event happens by calling a method on it. All lis teners must implemert the method the nv^nt s ource needs to crtl . To do so they deve to implement a listener interface.

An interface is like an abstract Java class, so it can't be instantiated. It defines a contract between the event source and the event listener. Classes that wart to listen for ActionEvents, for example, must implement the

ActionLi stener interface. All listener interfaces must extend the j ava .util. EventListener interface.

Classes that act as event sources must define methods that allow event listeners to register themselves. These methods typically take the form addXXListener (XXListener listener) and removeXXListener (XXListener listener), where XX is the event name. The class maintains a list of listeners registered for that event. When the event occurs, the event source class has the code to notify them.

The individual methods in the listener interface need only one argument, of type Event. For example, XXListener's methods take one ¡argument of XXEvent. Let's ssy you've written s class that monitors stock prices (we'll call it StockPri ceChecker), which acts as an event source for an event called StockPri ce. As the stoitIn jptictb rises aod fatls above or below certain letele, the dass sendy out StockPri ce events So all classes that are registered to rscnive them.

TheStockPri ceChecker class has hue following methods nor" tegistering StockPri ceEvent listener objects:

• addStockPriceListener(StockPriceListener listener)

• removeStockPriceListener(StockPriceListener listener)

TheStockPri ceEvent class msght have the hollfwipg methods that provide data dnthe StockPri ce event:

Contained in StockPri ceEvent is the duta thuss StockPri ceChecker sends out to the StockPrice event listeners.

TheStockPriceListener interfece can have the following methods, which define specific occurrences of events:

• priceDropped(StockPriceEvent event)

• priceIncreased(StockPriceEvent event)

Any class implementing the interface can register with the event sourd, StockPri ceChecker, to receive

StockPri ce events.

Was this article helpful?

0 0

Post a comment