Event Handling Notes
Event Handling Notes
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
Check box
Choice
List
Menu Item
Scroll bar
Text
Window
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)
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.