25-09-2013, 02:51 PM
Event Handling
Event Handling.pdf (Size: 194.98 KB / Downloads: 32)
Procedural vs. Event-Driven
Programming
• Procedural programming is executed in
procedural order.
• In event-driven programming, code is
executed upon activation of
events.
Events
• An event is an object that describes a state change in a
source.
• It can be generated as a consequence of a person
interacting with the elements in a GUI.
• Some of the activities that cause events to be generated
are pressing a button, entering a character via the
keyboard, selecting an item in a list, and clicking the
mouse.
• Events may also occur that are not directly caused by
interactions with a user interface.
For example, an event may be generated when a timer
expires, a counter exceeds a value, a software or
hardware failure occurs, or an operation is completed.
Event Classes in java.awt.event package
• ActionEvent: Generated when a button is pressed, a list item is
double clicked, or a menu item is selected.
• AdjustmentEvent: Generated when a scroll bar is manipulated.
• ComponentEvent: Generated when a component is hidden,
moved, resized, or becomes visible.
• ContainerEvent: Generated when a component is added to or
removed from a container.
• FocusEvent: Generated when a component gains or loses
keyboard focus.
• ItemEvent: Generated when a check box or list item is clicked; also
occurs when a choice selection is made or a checkable menu item is
selected or deselected.
• TextEvent: Generated when the value of a text area or text field is
changed.
• WindowEvent: Generated when a window is activated, closed,
deactivated, deiconified, iconified, opened, or quit.
Three Steps of Event Handling
1 Prepare to accept events
import package java.awt.event
2 Start listening for events
include appropriate methods
3 Respond to events
implement appropriate abstract method
Event Listeners
• A listener is an object that is notified when an event occurs.
• Event has two major requirements.
1. It must have been registered with one or more sources to
receive notifications about specific types of events.
2. It must implement methods to receive and process these
notifications.
• The methods that receive and process events are defined in a set of
interfaces found in java.awt.event.
• For example, the MouseMotionListener interface defines two
methods to receive notifications when the mouse is dragged or
moved.
• Any object may receive and process one or both of these events if it
provides an implementation of this interface.