Transcript Slide 1

Event Handling
The Event Delegation Model:
The Event Delegation Model:
•Event Handling is based on the delegation event model.
•It gives the mechanism to generate & process event.
•Event source generates an event and sends it to one or more listeners
•Listeners wait until it receives event.
•Once received the listeners process the event.
Events:
•Event is an object that shows change in state within a source.
•generated as sequence of person interactions with the elements
in graphical user interface.
•E.g. clicking a mouse button, the event object would contain
information such as the coordinates of the mouse cursor, which
button was clicked, how many times it was clicked, etc.
Event Source:
•Event source is an object that generates more than one type of
events.
•A source must register listeners for listeners to receive notifications
about a specific type of event.
General form of registration method
public void addTypeListener (TypeListener el)
where
•el is reference to the event listener.
•Type is name of event.
•The method that registers a mouse motion listener is called
addMouseMotionListener( ).
Event Source:
•When an event occurs, all registered listeners are notified and
receive a copy of the event object. This is known as multicasting
the event.
•In all cases, notifications are sent only to listeners that register to
receive them.
•Some sources may allow only one listener to register.
public void addTypeListener(TypeListener el)
throws java.util.TooManyListenersException
•When such an event occurs, the registered listener is notified.
This is known as unicasting the event.
Event Listeners
•A listener is an object that is notified when an event occurs.
• It 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.
Event Classes:
•The superclass for all events : EventObject, which is in java.util.
EventObject(Object src)
Here, src is the object that generates this event.
•EventObject contains two methods:
1.
getSource( ) : returns the source of the event.
Object getSource( )
2. toString( ) : returns the string equivalent of the event.
• AWTEvent, within the java.awt package: subclass of
EventObject.
•It is the superclass of all AWT-based events used by the delegation
event model.
Event Hierarchy in Java
java.lang.Object
|
+---java.util.EventObject
|
+---java.awt.AWTEvent
|
+---java.awt.event.ActionEvent
|
+---java.awt.event.ItemEvent
|
+---java.awt.event.AdjustmentEvent
|
+---java.awt.event.TextEvent
|
+----java.awt.event.ComponentEvent
|
+---java.awt.event.InputEvent
|
|
|
+---java.awt.event.KeyEvent
|
|
|
+---java.awt.event.MouseEvent
|
+---java.awt.event.FocusEvent
|
+---java.awt.event.ContainerEvent
|
+---java.awt.event.WindowEvent
The ActionEvent Class
An ActionEvent is generated when
A button is pressed
A list item is double-clicked
A menu item is selected.
ActionEvent(Object src, int type, String cmd)
src is a reference to the object that generated this event.
 The type of the event is specified by type
its command string is cmd.
String getActionCommand( ) When a button is pressed, an action event is
generated that has a command name equal to the
label on that button.
The AdjustmentEvent Class
•An AdjustmentEvent is generated by a scroll bar.
•Integer constants that can be used to identify events:
BLOCK_DECREMENT The user clicked inside the scroll bar to decrease its
value.
BLOCK_INCREMENT
The user clicked inside the scroll bar to increase its
value.
TRACK
The slider was dragged.
UNIT_DECREMENT
The button at the end of the scroll bar was clicked to
decrease its value.
UNIT_INCREMENT
The button at the end of the scroll bar was clicked to
increase its
value.
The AdjustmentEvent Class
int getAdjustmentType( )
 The type of the adjustment event
 It returns one of the constants
defined by AdjustmentEvent.
The ItemEvent Class:
An ItemEvent is generated when a check box or a list item is clicked
or when a checkable menu item is selected or deselected
•DESELECTED : The user deselected an item.
•SELECTED: The user selected an item.
Object getItem( )
used to obtain a reference to the item
that generated an event.
int getStateChange( )
returns the state change (that is,
SELECTED or DESELECTED) for
the event.
The KeyEvent Class:
AKeyEvent is generated when keyboard input occurs.
1. KEY_PRESSED
2. KEY_RELEASED
3. KEY_TYPED.
Integer constants that are defined by KeyEvent. :
VK_0 through VK_9 and VK_A through VK_Z
Here are some others:
VK_ALT , VK_DOWN , VK_LEFT , VK_RIGHT , VK_CANCEL
VK_ENTER , VK_PAGE_DOWN , VK_SHIFT , VK_CONTROL
VK_ESCAPE , VK_PAGE_UP , VK_UP
The VK constants specify virtual key codes.
The KeyEvent Class:
char getKeyChar( ) : char entered
int getKeyCode( ) : returns key code
The MouseEvent Class:
MOUSE_CLICKED
: The user clicked the mouse.
MOUSE_DRAGGED : The user dragged the mouse.
MOUSE_ENTERED
: The mouse entered a component.
MOUSE_EXITED
: The mouse exited from a component.
MOUSE_MOVED
: The mouse moved.
MOUSE_PRESSED
: The mouse was pressed.
MOUSE_RELEASED : The mouse was released.
MOUSE_WHEEL
: The mouse wheel was moved.
1. int getX( ) & int getY( ) : Return the X and Y coordinates of
the mouse within the component when the event occurred.
2. Point getPoint( ) : returns a Point object that contains the X,Y .
Sources of Events
Event Listener Interfaces:
Event Listener Interfaces:
ActionListener Interface:
1. void actionPerformed(ActionEvent ae)
AdjustmentListener Interface:
1. void adjustmentValueChanged(AdjustmentEvent ae)
ComponentListener Interface
1. void componentResized(ComponentEvent ce)
2. void componentMoved(ComponentEvent ce)
3. void componentShown(ComponentEvent ce)
4. void componentHidden(ComponentEvent ce)
Event Listener Interfaces:
ContainerListener Interface:
1. void componentAdded(ContainerEvent ce)
2. void componentRemoved(ContainerEvent ce)
FocusListener Interface:
1. void focusGained(FocusEvent fe)
2. void focusLost(FocusEvent fe)
ItemListener Interface:
1. void itemStateChanged(ItemEvent ie)
Event Listener Interfaces:
KeyListener Interface
1. void keyPressed(KeyEvent ke)
2. void keyReleased(KeyEvent ke)
3. void keyTyped(KeyEvent ke)
MouseListener Interface
1. void mouseClicked(MouseEvent me)
2. void mouseEntered(MouseEvent me)
3. void mouseExited(MouseEvent me)
4. void mousePressed(MouseEvent me)
5. void mouseReleased(MouseEvent me)
Event Listener Interfaces:
MouseMotionListener Interface
1.
void mouseDragged(MouseEvent me)
2.
void mouseMoved(MouseEvent me)
TextListener Interface:
1.
void textChanged(TextEvent te)
WindowListener Interface
1.
void windowActivated(WindowEvent we)
2.
void windowClosed(WindowEvent we)
3.
void windowClosing(WindowEvent we)
4.
void windowDeactivated(WindowEvent we)
5.
void windowDeiconified(WindowEvent we)
6.
void windowIconified(WindowEvent we)
7.
void windowOpened(WindowEvent we)
// Demonstrate the mouse event handlers.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="MouseEvents" width=300
height=100>
</applet>
*/
public class MouseEvents extends Applet
implements MouseListener,
MouseMotionListener
{
String msg = "";
int mouseX = 0, mouseY = 0;
public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
public void mouseClicked(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "Mouse clicked.";
repaint();
}
public void mouseEntered(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "Mouse entered.";
repaint();
}
public void mouseExited(MouseEvent me)
{
mouseX = 0;
mouseY = 10;
msg = "Mouse exited.";
repaint();
}
public void mousePressed(MouseEvent me)
public void mouseDragged(MouseEvent me) {
{
mouseX = me.getX();
mouseX = me.getX();
mouseY = me.getY();
mouseY = me.getY();
msg = "*";
msg = "Down";
showStatus("Dragging mouse at " + mouseX +
repaint();
", " + mouseY);
}
repaint();
public void mouseReleased(MouseEvent me)
}
{
mouseX = me.getX();
public void mouseMoved(MouseEvent me)
mouseY = me.getY();
{
msg = "Up";
showStatus("Moving mouse at " + me.getX() + ",
repaint();
}
" + me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg, mouseX, mouseY);
}
}
// Demonstrate the key event handlers.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="SimpleKey" width=300
height=100>
</applet>
*/
public class SimpleKey extends Applet
implements KeyListener {
String msg = "";
int X = 10, Y = 20;
public void init() {
addKeyListener(this);
requestFocus( ); // request input focus
}
public void keyPressed(KeyEvent ke)
{
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke)
{
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke)
{
msg += ke.getKeyChar();
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, X, Y);
}
}
Adapter Classes
•Java provides a special feature, called an adapter class that can
simplify the creation of event handlers in certain situations.
•An adapter class provides an empty implementation of all methods
in an event listener interface.
• Adapter classes are useful when you want to receive and process
only some of the events that are handled by a particular event
listener interface.
• User can extend one of the adapter classes and implement only
those events in which interested.
// Demonstrate an adaptor.
class MyMouseAdapter extends MouseAdapter
import java.applet.*;
{
import java.awt.event.*;
MousePressedDemo m1;
/*
public MyMouseAdapter(AdapterDemo m1)
<applet code="AdapterDemo" width=200
{
height=100>
this.m1 = m1;
</applet>
}
*/
public class AdapterDemo extends Applet
public void mousePressed(MouseEvent me)
{
{
m1.showStatus("Mouse Pressed.");
public void init()
{
MyMouseAdapter(this));
}
}
}
addMouseListener(new
}
Inner Classes
•The above example does not use an inner class.
•Its goal is to display the string “Mouse Pressed” in the status bar
of the applet viewer or browser when the mouse is pressed.
•There are two top-level classes in this program.
•AdaperDemo extends Applet, and MyMouseAdapter extends
MouseAdapter.
•The init( ) method of AdapterDemo instantiates
MyMouseAdapter and provides this object as an argument to
the addMouseListener( ) method.
Inner Classes
•A reference to the applet is supplied as an argument to the
MyMouseAdapter constructor.
•This reference is stored in an instance variable for later use by the
mousePressed( ) method.
•When the mouse is pressed, it invokes the showStatus( ) method of
the applet through the stored applet reference.
• In other words, showStatus( ) is invoked relative to the applet
reference stored by MyMouseAdapter.
// Inner class demo
import java.applet.*;
import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet
{
public void init() {
addMouseListener(new MyMouseAdapter());
}
class MyMouseAdapter extends MouseAdapter{
public void mousePressed(MouseEvent me){
showStatus("Mouse Pressed");
}
}
}