Objectives Event Classes | Komputasi | Suatu Permulaan

J.E.D.I. 8 GUI Event Handling

8.1 Objectives

In this module, you will learn how to handle events triggered when the user interacts with your GUI application. After completing this module, youll be able to develop GUI applications that responds to user interaction. After completing this lesson, you should be able to: 1. Enumerate the delegation event model components 2. Explain how the delegation event model works 3. Create GUI applications that interact with the user 4. Discuss benefits of adapter classes 5. Discuss advantages of using inner and anonymous classes

8.2 The Delegation Event Model

The Delegation event model describes how your program can respond to user interaction. To understand the model, let us first study its three important components. 1. Event Source The event source refers to the GUI component that generates the event. For example, if the user presses a button, the event source in this case is the button. 2. Event ListenerHandler The event listener receives news of events and processes users interaction. When a button is pressed, the listener may handle this by displaying an information useful to the user. 3. Event Object When an event occurs i.e., when the user interacts with a GUI component, an event object is created. The object contains all the necessary information about the event that has occurred. The information includes the type of event that has occurred, say, the mouse was clicked. There are several event classes for the different categories of user action. An event object has a data type of one of these classes. Introduction to Programming II Page 106 J.E.D.I. Here now is the delegation event model. Figure 8.1: Delegation Event Model Initially, a listener should be registered with a source so that it can receive information about events that occur at the source. Only registered listeners can receive notifications of events. Once registered, a listener simply waits until an event occurs. When something happens at the event source, an event object describing the event is created. The event is then fired by the source to the registered listeners. Once the listener receives an event object i.e., a notification from the source, it goes to work. It deciphers the notification and processes the event that occurred.

8.2.1 Registration of Listeners

The event source registers a listener through the addTypeListener methods. void addTypeListenerTypeListener listenerObj Type depends on the type of event source. It can be Key, Mouse, Focus, Component, Action and others. Several listeners can register with one event source to receive event notifications. Introduction to Programming II Page 107 J.E.D.I. A registered listener can also be unregistered using the removeTypeListener methods. void removeTypeListenerTypeListener listenerObj

8.3 Event Classes

An event object has an event class as its reference data type. At the root of event class heirarchy is the EventObject class, which is found in the java.util package. An immediate subclass of the EventObject class is the AWTEvent class. The AWTEvent class is defined in java.awt package. It is the root of all AWT-based events. Here are some of the AWT event classes. Event Class Description ComponentEvent Extends AWTEvent. Instantiated when a component is moved, resized, made visible or hidden. InputEvent Extends ComponentEvent. The abstract root event class for all component-level input event classes. ActionEvent Extends AWTEvent. Instantiated when a button is pressed, a list item is double-clicked, or a menu item is selected. ItemEvent Extends AWTEvent. Instantiated when an item is selected or deselected by the user, such as in a list or a checkbox. KeyEvent Extends InputEvent. Instantiated when a key is pressed, released or typed. MouseEvent Extends InputEvent. Instantiated when a mouse button is pressed, released, or clicked pressed and released, or when a mouse cursor enteres or exits a visible part of a component. TextEvent Extends AWTEvent. Instantiated when the value of a text field or a text area is changed. WindowEvent Extends ComponentEvent. Instantiated when a Window object is opened, closed, activated, deactivated, iconified, deiconified, or when focus is transferred into or out of the window. Table 23: Event classes Take note that all AWTEvent subclasses follow this naming convention: TypeEvent Introduction to Programming II Page 108 J.E.D.I.

8.4 Event Listeners