EVENT HANDLING MODEL
- GUIs
are event driven (i.e. they generate events when the user of the program
interacts with the GUI). Some common interactions are moving the mouse,
clicking the mouse, clicking a button, typing in a text field, selecting
an item from a menu, closing a window etc.
- When a
user interaction occurs, an event is sent to the program. GUI event
information is stored in an object of a class that extends AWTEvent.
- AWTEvents
are used with both AWT components and Swing components.
Java.lang.Object Action Event Container Event
Java.util.EventObject Adjustment Event Focus Event
Java.awt.AWTEvent Item Event Paint Event
Component
Event Window Event
Input
Event
Key
Event Mouse Event
- There
are three parts to the event handling mechanism
- Event
Source
- Event
Object
- Event
Listener
- The
event source is the particular GUI component with which the user
interacts. The event object encapsulates information about the event that
occurred. This information includes a reference to the event source and
any event-specific information that may be required by the event listener
to handle the event.
- The
event listener is an object that is notified by the event source when and
event occurs. The event listener receives an event object when it is
notified of the event, then uses the object to respond to the event. The event
source is required to provide methods that enable listeners to be
registered and unregistered. The event source also is required to maintain
a list of its registered listeners and be able to notify its listeners
when an event occurs.
- The
programmer must perform two key tasks to process a graphical user
interface event in a program. 1) Register an event listener for the GUI
component that is expected to generate the event. 2) Implement and event
handling method (or set of event-handling methods), called event
handlers.
- An
event listener for a GUI event is an object of a class that implements one
or more of the event-listener interfaces from package java.awt.evetn and
package javax.swing.event. Many of the event-listener are common to both
packages. The main event that all listeners extend from is the java.util.EventListener
.
Additional event listeners can be
found in the javax.swing.event package.
- An
event listener object listens for specific types of events generated by
event sources in a program. An event handler is a method that is called in
response to a particular type of event. Each event interface specifies one
or more event-handling methods that must be defined in the class
that implements the event-listener interface. REMEMBER that interfaces
define abstract methods. Any class that implements an interface
must define all the methods of that interface.
- When
an event occurs, the GUI component with which the user interacted notifies
its registered listeners by calling each listeners appropriate event
handling method. For example, when the user presses the Enter Key in a JTextField,
the registered listener’s actionPerformed method is called.
Example