Introduction Applets are event-driven programs. Event handling is at the core of successful applet programming. Most events to which an applet will respond are generated by the user. There are several types of events. The most commonly handled events are those generated by the mouse, the keyboard, and various controls, such as a push button. For example, when the user clicks on the mouse, the environment generates an event that it sends to the program. The program must then figure out what the mouse click means and act accordingly. Events are supported by the java.awt.event package. The way in which events are handled by an applet changed significantly between the original version of Java (1.0) and modern versions of Java, beginning with version 1.1. The 1.0 method of event handling is still supported, but it is not recommended for new programs. Also, many of the methods that support the old 1.0 event model have been deprecated. The Delegation Event Model The modern approach to handling events is based on the delegation event model. A source generates an event and sends it to one or more listeners. The listener simply waits until it receives an event. Once received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. Listeners must register with a source in order to receive an event notification. This provides an important benefit which is that notifications are sent only to listeners that want to receive them. Event An event is an object that describes a state change in a source. It can be generated as a consequence of a person interacting with the elements in a graphical user interface. Some of the activities that cause events are pressing a button, entering a character via the keyboard, selecting an item in a list, and clicking the mouse, etc. Events may also occur that are not directly caused by interactions with a user interface. For example, an event may be generated when a timer expires, a counter exceeds a value, software or hardware failure occurs, or an operation is completed. Event Sources A source is an object that generates an event. This occurs when the internal state of that object changes in some way. Sources may generate more than one type of event. A source must register listeners in order for the listeners to receive notifications about a specific type of event. Each type of event has its own registration method. Following is the general form: public void addTypeListener (TypeListener el) Here, Type is the name of the event and el is a reference to the event listener. For example, the method that registers a keyboard event listener is called addKeyListener( ). The method that registers a mouse motion listener is called addMouseMotionListener(). When an event occurs, all registered listeners are notified and receive a copy of the event object. This is known as multicasting the event. Notifications are sent only to the listeners that register to receive those events. Some event sources allow only one listener to register. The general form of such a method is as follows: public void addTypeListener(TypeListener el) throws java.util.TooManyListenersException Here, Type is the name of the event and el is a reference to the event listener. When such an event occurs, the registered listener is notified. This is known as unicasting the event. A source also provides a method that allows a listener to unregister a listener in a specific type of event. The general form of such a method is as follows: public void removeTypeListener(TypeListener el) Here, Type is the name of the event and el is a reference to the event listener. For example, to remove a keyboard listener, you would call removeKeyListener( ). The methods that add or remove listeners are provided by the source that generates events. For example, the Component class provides methods to add and remove keyboard and mouse event listeners. Event Listeners A listener is an object that is notified when an event occurs. It has two major requirements. First, it must have been registered with one or more sources to receive notifications about specific types of events. Second, it must implement methods to receive and process these notifications. The methods that receive and process events are defined in a set of interfaces found in java.awt.event. For example, the MouseMotionListener interface defines two methods to receive notifications when the mouse is dragged or moved. Any object may receive and process one or both of these events if it provides an implementation of this interface. Event Classes At the core of Java’s event handling mechanism are the classes that represent events. In this article I will introduce the various event classes. At the root of the Java event class hierarchy is EventObject, which is in java.util. It is the superclass for all events. Its one constructor is shown here: EventObject(Object src):- Here, src is the object that generates this event. EventObject contains two methods: getSource( ) and toString( ). The getSource( ) method returns the source of the event. Its general form is as follows: Object getSource( ) toString( ) method returns the string equivalent of the event. AWTEvent is a class defined within the java.awt package. This class is a subclass of EventObject. It is the superclass (either directly or indirectly) of all AWT-based events are handled by the delegation event model. It has a getID( ) method that can be used to determine the type of the event. The prototype of this method is as follows: int getID( ) The package java.awt.event defines several types of events that are generated by various user interface elements. Following are some of the important classes:- ActionEvent -- Generated when a button is pressed, a list item is double-clicked, or a menu item is selected. AdjustmentEvent -- Generated when a scroll bar is manipulated. ComponentEvent -- Generated when a component is hidden, moved, resized, or becomes visible. ContainerEvent -- Generated when a component is added to or removed from a container. FocusEvent -- Generated when a component gains or loses keyboard focus. InputEvent -- Abstract super class for all component input event classes. ItemEvent -- Generated when a check box or list item is clicked; also when a choice selection is made or a checkable menu item is selected or deselected. KeyEvent -- Generated when input is received from the keyboard. MouseEvent -- Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component. MouseWheelEvent -- Generated when the mouse wheel is moved. TextEvent -- Generated when the value of a text area or text field is changed. WindowEvent -- Generated when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. Sources of Events Some of the event sources are as follows: Button -- Generates action events when the button is pressed. Checkbox -- Generates item events when the check box is selected or deselected. Choice -- Generates item events when the choice is changed. List -- Generates action events when an item is double-clicked; generates item events when an item is selected or deselected. Menu Item -- Generates action events when a menu item is selected; generates item events when a checkable menu item is selected or deselected. Scrollbar -- Generates adjustment events when the scroll bar is manipulated. Textcomponents -- Generates text events when the user enters a character. Window -- Generates window events when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. Event Listener Interfaces Some of the Event listener interfaces are as follows: ActionListener -- Defines one method to receive action events. AdjustmentListener -- Defines one method to receive adjustment events. ComponentListener -- Defines four methods to recognize when a component is hidden, moved, resized, or shown. ContainerListener -- Defines two methods to recognize when a component is added to or removed from a container. FocusListener -- Defines two methods to recognize when a component gains or loses keyboard focus. ItemListener -- Defines one method to recognize when the state of an item changes. KeyListener -- Defines three methods to recognize when a key is pressed, released, or typed. MouseListener -- Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released. MouseMotionListener -- Defines two methods to recognize when the mouse is dragged or moved. MouseWheelListener -- Defines one method to recognize when the mouse wheel is moved. TextListener -- Defines one method to recognize when a text value changes. WindowFocusListener -- Defines two methods to recognize when a window gains or loses input focus. WindowListener -- Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit.