- In this section, we examine how to make your
programs respond to events. In the most basic terms, events occur
when something happens. The most common events are caused by user
interaction. For example, events occur when a user presses the mouse
button, releases the mouse button, or presses a key on the
- Events are also generated without user
involvement, such as when code programmatically selects items and
generates an event.
- After an event occurs, it's then up to your
program to determine the appropriate response.
- User-interface components can both listen
for events such as key and mouse presses, and also generate events of
their own. For example, a button listens for a mouse press and
release that occurs over itself. When one occurs, the button posts
its own event (an action event).
- Dealing with events can be complicated because
there are two different event models to choose from since the JDK
changed the way events were handled between the JDK 1.0 and JDK 1.1
- The JDK 1.0 release from Sun contains a very
simple method of event handling. Events are sent to the component
where they occur and then sent to that component's parents. You can
simply subclass a component or one of the component's parents to
implement a method to handle events.
- The JDK 1.1 release introduced a new way to
deal with events. In the 1.1 model, events are distributed using
delegation. Objects register themselves as being interested in
receiving events from other objects. The JDK 1.1 model uses listeners
and adapters that allow you to separate user-interface and event-handling
code. It also allows any object, not just a user-interface component, to
receive events from a component.