Java Reference
In-Depth Information
OKListenerClass is invoked to process the event. The object listener2 is an instance of
CancelListenerClass (line 19), which is registered with the button jbtCancel in line
21. When the Cancel button is clicked, the actionPerformed(ActionEvent) method
(line 43) in CancelListenerClass is invoked to process the event.
You now have seen a glimpse of event-driven programming in Java. You probably have
many questions, such as why a listener class is defined to implement the ActionListener .
The following sections will give you all the answers.
16.2 Events and Event Sources
An event is an object created from an event source. Firing an event means to create an
event and delegate the listener to handle the event.
Key
Point
When you run a Java GUI program, the program interacts with the user, and the events drive
its execution. This is called event-driven programming. An event can be defined as a signal to
the program that something has happened. Events are triggered either by external user
actions, such as mouse movements, button clicks, and keystrokes, or by internal program
activities, such as a timer. The program can choose to respond to or ignore an event. The
example in the preceding section gave you a taste of event-driven programming.
The component that creates an event and fires it is called the event source object , or simply
source object or source component . For example, a button is the source object for a button-
clicking action event. An event is an instance of an event class. The root class of the event
classes is java.util.EventObject . The hierarchical relationships of some event classes
are shown in Figure 16.4.
event-driven programming
event
fire event
event source object
source object
ActionEvent
ContainerEvent
AdjustmentEvent
FocusEvent
MouseEvent
EventObject
AWTEvent
ComponentEvent
InputEvent
ItemEvent
PaintEvent
KeyEvent
TextEvent
WindowEvent
ListSelectionEvent
ChangeEvent
F IGURE 16.4
An event is an object of the EventObject class.
An event object contains whatever properties are pertinent to the event. You can identify
the source object of an event using the getSource() instance method in the EventObject
class. The subclasses of EventObject deal with specific types of events, such as action
events, window events, component events, mouse events, and key events. The first three
columns in Table 16.1 list some external user actions, source objects, and event types fired.
For example, when clicking a button, the button creates and fires an ActionEvent , as indi-
cated in the first line of this table. Here the button is an event source object and an
ActionEvent is the event object fired by the source object, as shown in Figure 16.2.
event object
getSource()
Note
If a component can fire an event, any subclass of the component can fire the same type
of event. For example, every GUI component can fire MouseEvent and KeyEvent ,
since Component is the superclass of all GUI components.
 
 
Search WWH ::




Custom Search