Java Events: J Il J Java - Util. Java - Awt.event
Java Events: J Il J Java - Util. Java - Awt.event
Introduction
•Java programming provides event handling
•Events are generated by users or systems
•Applets or other java applications are events driven programming
•Java provides following two packages to handle events
java.util.*; java.awt.event;
•Events are passed to the application in many ways like mouse , key, controls etc.
1
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
The Delegation Event Model :
Generates Registered with source
S
Source E t
Event Li t
Listener
Return Process
Result return back to source
Li t
Listener process event
t
2
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
The Delegation Event Model : Advantages
pp g p y p
•Application logic that processes events is clearly separated from the user interface
logic that generate those events
•User Interface element is able to delegate the processing of an event to a separate
piece of code
•Listener must register with a source in order to receive an event notification
•Notifications are sent only to listener that want to receive them
3
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Old Event Model :
p p g p y y p
•Event will propagated up the containment hierarchy unless handled by a component
•This requires components to receive events that they did not process
4
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event :
j g
•Event is an object that describe a state change in a source
•Event may also occurs not directly caused by user interaction
Example :
Timer expiration
Counter exceeds
5
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event Source :
• Source is an object that generates an event
j g
•Source may generate more than one type of event
•Source must register listener in order for the listener to receive notification about event
Example :
Example :
Button
Text
6
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event Listener :
• Listener is an object that is notifies when an event occurs
• Listener can be registered with one or more source to receive notification
about specific types of event
• Listener must implement methods to receive and process the notification
7
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event Classes : Java.util.*
il *
EventObject (Object src)
Class
getSource()
Constructor EventObject
Sub Class toString()
Object that
Java.awt.*
generates the
generates the
event
AWTEvent
Methods
getID()
8
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event Classes Example :
Event Class Generated when
ActionEvent ,
Button, List or Menu Item is clicked
FocusEvent component gains or loses focus
I
ItemEvent
E checkBox, choice are selected
h kB h i l d
InputEvent input
KeyEvent input received from the keyboard
MouseEvent mouse draged, entered, etc
WindowEvent window open, close
Note : for more classes, methods and interfaces please refer JAVA2 Complete Reference
9
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event Sources Example :
Event Class Generated event
CheckBox Checkbox is selected or deselected
Choice Choice is changed
MenuItem Menu Item is selected
Window Window is activated, deactivated etc
Note : for more classes, methods and interfaces please refer JAVA2 Complete Reference
10
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event Listener Interfaces :
Event Class Defines methods to
ActionListener Receive action events
KeyListener Key Pressed, Released or Typed
MouseMotionListener Dragged or moved
Note : for more classes, methods and interfaces please refer JAVA2 Complete Reference
11
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Event Handling Mechanism
Event Handling Example:
Import java.awt.*;
Import java.awt.event.*; Listener
Import java.applet.*;
p j pp ;
Public class Example extends Applet implements MouseMotionListener{
public void init(){
Registering setBackground(Color.blue);
listener to setForeground(Color.yelow); Source is applet
the source addMouseMotionListener(this);
}
Public void mouseDragged(MouseEvent e)
{
} Override methods
Public void mouseMoved(MouseEvent e)
{
}
}
12
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Adapter Classes
p p py p
•Adapter Class provides an empty implementation of all methods in an event listener interface
•Adapter classes are useful when you want to process only some of the events
•Define a new class that act as an event listener by extending one of the adapter class
•Implement only those events which is required
13
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Adapter Classes
List of Adapter Classes and its Listener Interface
Adapter Classes Listener Interface
ComponentAdapter ComponentListener
ContainerAdapter ContainerListener
FocusAdapter
p FocusListener
KeyAdapter KeyListener
M
MouseAdapter
Ad t M
MouseListener
Li t
MouseMotionAdapter MouseMotionListener
WindowAdapter WindowListener
14
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
Adapter Classes
Adapter Class Example:
Import java.awt.*;
Import java.awt.event.
Import java awt event *;;
Import java.applet.*;
public class TestApplet extends Applet{
public void init(){
public void init(){
Applet class
Class extending setBackground(Color.pink);
adapter class setSize(500,300);
addMouseListener(new SampleAdapterClass(this));
( p p ( ));
}
} Register Applet with
class SampleAdapterClass extends MouseAdapter { New instance of
TestApplet t; Adapter class
SampleAdapterClass (TestApplet d){
this.t = d; Adapter class constructor
p
} initializing Applet class
public void mouseClicked(MouseEvent e){
t.showStatus("is working");
} Mouse Listeners only method
Mouse Listeners only method
} processed
15
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “
JAVA EVENTS
GLOSSARY
Overview of the terms mentioned in this Chapter
•Java Event Handling Concept
•Packages to handle events
•Delegation Event model
•Definition of Event , Event Source Event Classes and Event Listener
f l d
•List of Event classes, Event Listeners and Event Sources
•Adapter Classes
References :
Java 2 The Complete Reference, Herbert Schildt, TATA McGraw Hill
16
©Zaheeruddin Ahmed “ Save paper ...! do not print unless necessary. “