19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

602 Chapter 16 Event-Driven <strong>Programming</strong><br />

OKListenerClass is invoked <strong>to</strong> process the event. The object listener2 is an instance of<br />

CancelListenerClass (line 19), which is registered with the but<strong>to</strong>n jbtCancel in line<br />

21. When the Cancel but<strong>to</strong>n is clicked, the actionPerformed(ActionEvent) method<br />

(line 43) in CancelListenerClass is invoked <strong>to</strong> process the event.<br />

You now have seen a glimpse of event-driven programming in <strong>Java</strong>. You probably have<br />

many questions, such as why a listener class is defined <strong>to</strong> implement the ActionListener.<br />

The following sections will give you all the answers.<br />

event-driven programming<br />

event<br />

fire event<br />

event source object<br />

source object<br />

Key<br />

Point<br />

16.2 Events and Event Sources<br />

An event is an object created from an event source. Firing an event means <strong>to</strong> create an<br />

event and delegate the listener <strong>to</strong> handle the event.<br />

When you run a <strong>Java</strong> GUI program, the program interacts with the user, and the events drive<br />

its execution. This is called event-driven programming. An event can be defined as a signal <strong>to</strong><br />

the program that something has happened. Events are triggered either by external user<br />

actions, such as mouse movements, but<strong>to</strong>n clicks, and keystrokes, or by internal program<br />

activities, such as a timer. The program can choose <strong>to</strong> respond <strong>to</strong> or ignore an event. The<br />

example in the preceding section gave you a taste of event-driven programming.<br />

The <strong>com</strong>ponent that creates an event and fires it is called the event source object, or simply<br />

source object or source <strong>com</strong>ponent. For example, a but<strong>to</strong>n is the source object for a but<strong>to</strong>nclicking<br />

action event. An event is an instance of an event class. The root class of the event<br />

classes is java.util.EventObject. The hierarchical relationships of some event classes<br />

are shown in Figure 16.4.<br />

ActionEvent<br />

AdjustmentEvent<br />

ContainerEvent<br />

FocusEvent<br />

MouseEvent<br />

EventObject<br />

AWTEvent<br />

ComponentEvent<br />

InputEvent<br />

ItemEvent<br />

TextEvent<br />

PaintEvent<br />

WindowEvent<br />

KeyEvent<br />

ListSelectionEvent<br />

ChangeEvent<br />

FIGURE 16.4<br />

event object<br />

getSource()<br />

An event is an object of the EventObject class.<br />

An event object contains whatever properties are pertinent <strong>to</strong> the event. You can identify<br />

the source object of an event using the getSource() instance method in the EventObject<br />

class. The subclasses of EventObject deal with specific types of events, such as action<br />

events, window events, <strong>com</strong>ponent events, mouse events, and key events. The first three<br />

columns in Table 16.1 list some external user actions, source objects, and event types fired.<br />

For example, when clicking a but<strong>to</strong>n, the but<strong>to</strong>n creates and fires an ActionEvent, as indicated<br />

in the first line of this table. Here the but<strong>to</strong>n is an event source object and an<br />

ActionEvent is the event object fired by the source object, as shown in Figure 16.2.<br />

Note<br />

If a <strong>com</strong>ponent can fire an event, any subclass of the <strong>com</strong>ponent can fire the same type<br />

of event. For example, every GUI <strong>com</strong>ponent can fire MouseEvent and KeyEvent,<br />

since Component is the superclass of all GUI <strong>com</strong>ponents.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!