Oop Events
Oop Events
Ali Akbar N
The Delegation Event Model
●
Source -generates an event and sends it to one or
more listeners.
●
Listener -simply waits until it receives an event.
Once an event is received, the listener processes
the event and then returns.
●
A user interface element is able to “delegate” the
processing of an event to a separate piece of code.
Advantages
●
The application logic that processes events is
cleanly separated from the user interface logic that
generates those events.
●
Listeners must register with a source in order to
receive an event notification. So notifications are
sent only to listeners that want to receive them.
Events
●
An event is an object that describes a state change in a
source.
●
An event can be generated as a consequence of a
person interacting with the elements in a GUI.
eg. pressing a button, entering a character via the
keyboard, selecting an item in a list, clicking the
mouse etc.
●
Events may also occur that are not directly caused by
interactions with a user interface.
eg. when a timer expires, a counter exceeds a value, a
software or hardware failure occurs etc.
Event Sources
●
A source is an object that generates an event.
●
Sources may generate more than one type of event.
●
A source must register listeners in order for the listeners to receive
notifications about a specific type of event.
General form: public void addTypeListener (TypeListener el )
Here, Type is the name of the event, and el is a reference to the
event listener.
●
For example,
addKeyListener( )- method that registers a keyboard event listener.
●
A listener is an object that is notified when an event occurs.
●
It has two major requirements.
First, it must have been registered with one or more sources to
receive notifications about specific types of events.
Second, it must implement methods to receive and process these
notifications.
●
For example, the MouseMotionListener interface defines two
methods to receive notifications when the mouse is dragged or
moved.
Event Classes
●
The classes that represent events are at the core of
Java’s event handling mechanism.
●
The package java.awt.event defines many types of
events that are generated by various user interface
elements.
Some Event Classes
●
ActionEvent - Generated when a button is pressed, a list item is double-
clicked, or a menuitem is selected.
●
AdjustmentEvent -Generated when a scroll bar is manipulated.
●
ItemEvent -Generated when a check box or list item is clicked.
●
KeyEvent -Generated when input is received from the keyboard.
●
MouseEvent- Generated when the mouse is dragged, moved, clicked,
pressed, or released;
●
TextEvent -Generated when the value of a text area or text field is changed.
●
WindowEvent -Generated when a window is activated, closed, deactivated,
opened, or quit.
Example Program
●
The following program demonstrates keyboard
input. It echoes keystrokes to the applet window
and shows the pressed/released status of each key
in the status window.
Handling Keyboard Events
// Demonstrate the key event handlers.
import java.awt.*; public void keyPressed(KeyEvent ke) {
o u a y
Y e d
n k i c
h a a n
T ve
H a