A bean may communicate with other beans. The Java event delegation model provides the foundation for beans to send, receive, and handle events. Let us review the Java event model that was introduced in Chapter 14, "Event-Driven Programming." The Java event model consists of the following three types of elements, as shown in Figure 14.3:
The event object
The source object
The event listener object
An event is a signal to the program that something has happened . It can be triggered by external user actions, such as mouse movements, mouse button clicks, and keystrokes, or by the operating system, such as a timer. An event object contains the information that describes the event. A source object is where the event originates. When an event occurs on a source object, an event object is created. An object interested in the event receives the event. Such an object is called a listener . Not all objects can receive events. To become a listener, an object must be registered as a listener by the source object. The source object maintains a list of listeners and notifies all the registered listeners by invoking the event-handling method implemented on the listener object. The handlers are defined in the class known as the event listener interface . Each class of an event object has a corresponding event listener interface. The Java event model is referred to as a delegation-based model because the source object delegates the event to the listeners for processing.
An event object is created using an event class, such as ActionEvent , MouseEvent , and ItemEvent , as shown in Figure 14.2. All the event classes extend java.util.EventObject . The event class contains whatever data values and methods are pertinent to the particular event type. For example, the KeyEvent class describes the data values related to a key event and contains the methods, such as getKeyChar() , for retrieving the key associated with the event.
Every event class is associated with an event listener interface that defines one or more methods referred to as handlers . An event listener interface is a subinterface of java.util.EventListener . The handlers are implemented by the listener components . The source component invokes the listeners' handlers when an event is detected .
Since an event class and its listener interface are coexistent, they are often referred to as an event set or event pair . The event listener interface must be named X Listener for the X Event. For example, the listener interface for ActionEvent is ActionListener . The parameter list of a handler always consists of an argument of the event class type. Table 14.2 lists some commonly used events and their listener interfaces. Figure 27.2 shows the pair of ActionEvent and ActionListener .
The component on which an event is generated is referred to as an event source . Every Java GUI component is an event source for one or more events. For example, JButton is an event source for ActionEvent . A JButton object fires a java.awt.event.ActionEvent when it is clicked. JComboBox is an event source for ActionEvent and ItemEvent . A JComboBox object fires a java.awt.event.ActionEvent and a java.awt.event.ItemEvent when a new item is selected in the combo box.
The source component contains the code that detects an external or internal action that triggers the event. Upon detecting the action, the source should fire an event to the listeners by invoking the event handler defined by the listeners. The source component must also contain methods for registering and deregistering listeners, as shown in Figure 27.3.
A listener component for an event must implement the event listener interface. The object of the listener component cannot receive event notifications from a source component unless the object is registered as a listener of the source.
A listener component may implement any number of listener interfaces to listen to several types of events. A source component may register many listeners. A source component may register itself as a listener.
Listing 27.1 gives an example that creates a source object (line 8) and a listener object (line 14), and registers the listener with the source object (line 17). Figure 27.4 highlights the relationship between the source and the listener. The listener is registered with the source by invoking the addActionListener method. Once the button is clicked, an ActionEvent is generated by the source. The source object then notifies the listener by invoking the listener's actionPerformed method.
1 import javax.swing.*; 2 import java.awt.event.*; 3 4 public class TestSourceListener { 5 public static void main(String[] args) { 6 JFrame frame = new JFrame( "TestSourceListener" ); 7 // Create a source object 8 JButton jbt = new JButton( "OK" ); 9 frame.add(jbt); 10 frame.setSize( 200 , 200 ); 11 frame.setVisible( true ); 12 13 // Create listeners 14 MyListener listener = new MyListener(); 15 16 // Register listeners 17 jbt.addActionListener(listener); 18 } 19 } 20 21 /** MyListener class */ 22 class MyListener implements ActionListener{ 23 public void actionPerformed(ActionEvent e) { 24 System.out.println( "I will process it!" ); 25 } 26 } |