Event Handling In Java - Going Into Over Drive (
Page 2 of 7 )
Components of an
Event: Can be put under the following categories.
1.
Event
Object: When the user interacts with the application by clicking a mouse
button or pressing a key an event is generated. The Operating System traps this
event and the data associated with it. For example, info about time at which the
event occurred, the event types (like keypress or mouse click). This data is
then passed on to the application to which the event belongs. In Java, events
are represented by objects, which describe the events themselves. And Java has a
number of classes that describe and handle different categories of
events.
2.
Event Source: An event source is the object that
generated the event. Example if you click a button an ActionEvent Object is
generated. The object of the ActionEvent class contains information about the
event.
3.
Event-Handler: Is a method that understands the
event and processes it. The event-handler method takes the Event object as a
parameter. Java uses
Event-Delegation Model :with JDK1.1 onwards;
you can specify the objects that are to be notified when a specific event
occurs. If the event is irrelevant, it is discarded. The four main components
based on this model are
Event classes, Event Listeners, Explicit event
handling and Adapters. Let us take a closer look at them one by
one.
Event Classes: The EventObject class is at the top of the
event class hierarchy. It belongs to the
java.util package. While most of
the other event classes are present in java.awt.event package. The getSource()
method of the EventObject class returns the object that initiated the event. The
getId () method returns the nature of the event. For example, if a mouse event
occurs, you can find out whether the event was click, a press, a move or release
from the event object. AWT provides two conceptual types of events: Semantic and
low-level events.