Java Event Handling (Part-1)
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
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.
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.
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:
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
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:
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:
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
The methods that add or remove listeners are provided by the source that generates events. For example, the
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
For example, the
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
Its one constructor is shown here:
EventObject contains two methods:
The getSource( ) method returns the source of the event. Its general form is as follows:
toString( ) method returns the string equivalent of the event.
It has a getID( ) method that can be used to determine the type of the event. The prototype of this method is as follows:
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:-
Some of the event sources are as follows:
Some of the Event listener interfaces are as follows:
Re: Java Event Handling (Part-1)
Nominate this article for Article of the month - Apr 2010
Re: Java Event Handling (Part-1)
Vote for this article for Article of the month - Apr 2010
|All times are GMT +5.5. The time now is 04:13.|