0% found this document useful (0 votes)
126 views

Event Handling Notes

The document describes Java event handling and listener classes. It discusses: 1. The key event classes like ActionEvent, ItemEvent, KeyEvent, and WindowEvent and their methods. 2. Common listener interfaces like ActionListener, ItemListener, KeyListener etc and their callback methods. 3. How to use the delegation event model to implement listeners and register them to receive events.

Uploaded by

Imtiyaz Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

Event Handling Notes

The document describes Java event handling and listener classes. It discusses: 1. The key event classes like ActionEvent, ItemEvent, KeyEvent, and WindowEvent and their methods. 2. Common listener interfaces like ActionListener, ItemListener, KeyListener etc and their callback methods. 3. How to use the delegation event model to implement listeners and register them to receive events.

Uploaded by

Imtiyaz Shaikh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

ActionEvent Class

String getActionCommand( ) to get the command name


i.etext on the command
int getModifiers( ) to know which modifier key like ctrl, shift
etc was pressed
long getWhen( ) - returns the time at which the event took
place

ItemEvent Class
Object getItem( ) - used to obtain a reference to the item that
generated an event
ItemSelectable getItemSelectable( ) - used to obtain a
reference to the ItemSelectable object that generated an event.
int getStateChange( ) - returns the state change (that is,
SELECTED or
DESELECTED) for the event
KeyEvent Class
char getKeyChar( ) - returns the character that was entered
int getKeyCode( ) - returns the key code
MouseEvent Class
int getX( )
int getY( )
Point getPoint( )
void translatePoint(int x, int y)
int getClickCount( )
boolean isPopupTrigger( )
int getButton( )
WindowEvent Class
Window getWindow( )

Window getOppositeWindow( )
Int getOldState( )
int getNewState( )

EVENT Resources
Event Source

Description

Button

Generates action events when


the button is pressed.

Check box

Generates item events when


the check box is selected or
deselected.
Generates item events when
the choice is changed.

Choice

List

Menu Item

Scroll bar

Text

Window

Generates action events when


an item is double-clicked;
generates item events when an
item is selected or deselected.
Generates action events when a
menu item is selected;
generates item events when a
checkable menu item is
selected or deselected.
Generates adjustment events
when the scroll bar is
manipulated.
components Generates text
events when the user enters a
character.
Generates window events when

a window is activated, closed,


deactivated, deiconified,
iconified, opened, or quit.

Listeners
ActionListener
void actionPerformed(ActionEvent ae)
ItemListener
void itemStateChanged(ItemEvent ie)
KeyListener
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
MouseListener
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
MouseMotionListene
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
TextListener
void textChanged(TextEvent te)
WindowListener

void
void
void
void
void
void
void

windowActivated(WindowEvent we)
windowClosed(WindowEvent we)
windowClosing(WindowEvent we)
windowDeactivated(WindowEvent we)
windowDeiconified(WindowEvent we)
windowIconified(WindowEvent we)
windowOpened(WindowEvent we)

Using the Delegation Event Model


1. Implement the appropriate interface in the listener so that it
will receive the I type of event desired.
2. Implement code to register and unregister (if necessary) the
listener as a recipient for the event notifications.

Adapter and Inner classes


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.
You can define a new class to act as an event listener by
extending one of the adapter classes and implementing only
those events in which you are interested.
For example, the MouseMotionAdapter class has two methods,
mouseDragged( )
and mouseMoved( ), which are the methods defined by the
MouseMotionListener
interface. If you were interested in only mouse drag events, then
you could simply extend MouseMotionAdapter and override
mouseDragged( ). The empty implementation of mouseMoved( )
would handle the mouse motion events for you.
// Demonstrate an adapter.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="AdapterDemo" width=300 height=100>
</applet>
*/
public class AdapterDemo extends Applet {

public void init() {


addMouseListener(new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter {
AdapterDemo adapterDemo;
public MyMouseAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
// Handle mouse clicked.
public void mouseClicked(MouseEvent me) {
adapterDemo.showStatus("Mouse clicked");
}
}
class MyMouseMotionAdapter extends MouseMotionAdapter {
AdapterDemo adapterDemo;
public MyMouseMotionAdapter(AdapterDemo adapterDemo) {
this.adapterDemo = adapterDemo;
}
// Handle mouse dragged.
public void mouseDragged(MouseEvent me) {
adapterDemo.showStatus("Mouse dragged");
}
}
Inner Classes
There are two top-level classes in this program.
MousePressedDemo extends Applet, and MyMouseAdapter
extends MouseAdapter. The init( ) method of MousePressedDemo
instantiates MyMouseAdapter and provides this object as an
argument to the addMouseListener( ) method.
Notice that 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.
// This applet does NOT use an inner class.
import java.applet.*;
import java.awt.event.*;
/*
<applet code="MousePressedDemo" width=200 height=100>
</applet>
*/
public class MousePressedDemo extends Applet {
public void init() {
addMouseListener(new MyMouseAdapter(this));
}
}
class MyMouseAdapter extends MouseAdapter {
MousePressedDemo mousePressedDemo;
public MyMouseAdapter(MousePressedDemo mousePressedDemo)
{
this.mousePressedDemo = mousePressedDemo;
}
public void mousePressed(MouseEvent me) {
mousePressedDemo.showStatus("Mouse Pressed.");
}
}
The following listing shows how the preceding program can be
improved by using an inner class. Here, InnerClassDemo is a toplevel class that extends Applet. MyMouseAdapter is an inner class
that extends MouseAdapter. Because MyMouseAdapter is defined
within the scope of InnerClassDemo, it has access to all of the
variables and methods within the scope of that class. Therefore,
the mousePressed( ) method can call the showStatus( ) method
directly. It no longer needs to do this via a stored reference to the
applet. Thus, it is no longer necessary to pass MyMouseAdapter( )
a reference to the invoking object.
// Inner class demo.
import java.applet.*;

import java.awt.event.*;
/*
<applet code="InnerClassDemo" width=200 height=100>
</applet>
*/
public class InnerClassDemo extends Applet {
C h a p t e r 2 2 : E v e n t H a n d l i n g 661
public void init() {
addMouseListener(new MyMouseAdapter());
}
class MyMouseAdapter extends MouseAdapter {
public void mousePressed(MouseEvent me) {
showStatus("Mouse Pressed");
}
}
}
Anonymous Inner Classes
An anonymous inner class is one that is not assigned a name. This
section illustrates how an
anonymous inner class can facilitate the writing of event
handlers. Consider the applet shown
in the following listing. As before, its goal is to display the string
Mouse Pressed in the
status bar of the applet viewer or browser when the mouse is
pressed.
// Anonymous inner class demo.
import java.applet.*;
import java.awt.event.*;
/*
<applet code="AnonymousInnerClassDemo" width=200
height=100>
</applet>
*/
public class AnonymousInnerClassDemo extends Applet {
public void init() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
showStatus("Mouse Pressed");

}
});
}
}
There is one top-level class in this program:
AnonymousInnerClassDemo. The init( )
method calls the addMouseListener( ) method. Its argument is an
expression that defines
and instantiates an anonymous inner class. Lets analyze this
expression carefully.
The syntax new MouseAdapter( ) { ... } indicates to the compiler
that the code between the
braces defines an anonymous inner class. Furthermore, that class
extends MouseAdapter. This
new class is not named, but it is automatically instantiated when
this expression is executed.
Because this anonymous inner class is defined within the scope of
AnonymousInnerClassDemo, it has access to all of the variables
and methods within
the scope of that class. Therefore, it can call the showStatus( )
method directly.
As just illustrated, both named and anonymous inner classes
solve some annoying
problems in a simple yet effective way. They also allow you to
create more efficient code.

You might also like