Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
Whenever a user interacts with an applet (e.g. moving the mouse, clicking a mouse button or hitting a keyboard key) the applet calls an event handler method. We can override the various event handler methods so as to cause a user defined event to occur in response to a user's input.
You must import java.awt.event.* if you wish to use awt event handling in an applet.
Java only deals with events that have been registered by a class as being needed. Any event that is not registered is ignored by the applet (and uses no processing time). The ability to ignore certain events and register only those that we are interested in offers huge improvements in efficiency, as most classes will have no interest in using the vast majority of events that occur. In order to register an event as being needed, a class must implement the Interface that contains the required event handler method.
The various awt events that can be triggered by a user's actions are implemented within different Interfaces. Details of the various Interfaces that are used in these notes are shown below.
Interface |
Implemented Methods |
Parameter |
Parameter's Methods |
Generated By |
ActionListener |
actionPerformed |
ActionEvent |
getActionCommand getModifiers |
Button List MenuItem TextField |
AdjustmentListener |
adjustmentValueChanged |
AdjustmentEvent |
getAdjustable getAdjustmentType getValue |
Scrollbar |
ItemListener |
itemstateChanged |
ItemEvent |
getItem getItemSelectable getStateChange |
Checkbox CheckboxMenuItem Choice List |
TextListener |
textValueChanged |
TextEvent |
NONE |
TextComponent |
ComponentListener |
componentMoved componentHidden componentResized componentShown |
ComponentEvent |
getComponent |
Component |
ContainerListener |
componentAdded componentRemoved |
ContainerEvent |
getContainer getChild |
Container |
FocusListener |
focusGained focusLost |
FocusEvent |
isTempory |
Component |
KeyListener |
keyPressed keyReleased keyTyped |
KeyEvent |
getKeyChar getKeyCode getKeyModifiedsText getKeyText isActionKey |
Component |
MouseListener |
mousePressed mouseReleased mouseEntered mouseExited mouseClicked |
MouseEvent |
getClickCount getX getY getPoint translatePoint isPopuptrigger |
Component |
MouseMotionListener |
mouseDragged mouseMoved |
MouseEvent |
NONE |
Component |
WindowListener |
windowClosing windowOpened windowIconified windowDeiconified windowClosed windowActivated windowDeactivated |
WindowEvent |
getWindow |
Window |
All listener interfaces correspond to one AWT event type, with the exception of MouseListener and MouseMotionListener. Both of these correspond to a MouseEvent. This is done for efficiency. It is very common for a program to wish only to capture the events contained in the MouseListener interface (e.g. mouseClicked()). Most programs, however, never need to capture the events contained in MouseMotionListener. If MouseMotionListener is implemented, then every time the mouse is moved an event is triggered, leading to a processing overhead. By separating the commonly used MouseListener interface from the less commonly used MouseMotionListener interface we reduce the processing redundancy.
Event handling is done using interfaces. If we implement an interface then we need to override all methods that are contained in that interface. Alternatively, we can use inner classes to handle the events. Java provides a set of inner classes for that purpose. An inner class is provided for any interface that has more then one method. The inner classes provided are:
Interface |
Inner Class |
ComponentListener |
ComponentAdapter |
ContainerListener |
ContainerAdapter |
FocusListener |
FocusAdapter |
KeyListener |
KeyAdapter |
MouseListener |
MouseAdapter |
MouseMotionListener |
MouseMotionAdapter |
WindowListener |
WindowAdapter |
Inner classes are described in more detail in the section on "Java Basics, Inner Classes".
Various awt event handling examples are shown throughout the following notes.
If we are monitoring an event for more than one component it is not always advantageous to use inner classes. The three examples below show this.
Example without inner class (Run Applet).
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseListener_WITHOUT_InnerClassDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel implements MouseListener { Color oldColor; // the original label color public View() { super(); final JLabel label1 = new JLabel("Label 1"); final JLabel label2 = new JLabel("Label 2"); // remember the original label colour this.oldColor = label1.getForeground(); add(label1); add(label2); label1.addMouseListener(this); label2.addMouseListener(this); } @Override public void mousePressed(MouseEvent e) { e.getComponent().setForeground(Color.green); } @Override public void mouseReleased(MouseEvent e) { e.getComponent().setForeground(this.oldColor); } // we need to overwrite all methods of the implemented class @Override public void mouseClicked(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } } }
Example with inner class (Run Applet).
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseListener_WITH_InnerClassDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { public View() { super(); final JLabel label1 = new JLabel("Label 1"); final JLabel label2 = new JLabel("Label 2"); this.add(label1); this.add(label2); label1.addMouseListener(new MouseAdapter() { Color oldColor; // the original label color @Override public void mousePressed(MouseEvent e) { oldColor = e.getComponent().getForeground(); e.getComponent().setForeground(Color.red); } @Override public void mouseReleased(MouseEvent e) { e.getComponent().setForeground(this.oldColor); } }); label2.addMouseListener(new MouseAdapter() { Color oldColor; // the original label color @Override public void mousePressed(MouseEvent e) { oldColor = e.getComponent().getForeground(); e.getComponent().setForeground(Color.red); } @Override public void mouseReleased(MouseEvent e) { e.getComponent().setForeground(this.oldColor); } }); } } }
A third approach is to make a member variable instance of the FocusAdapter class, and to pass this to the addFocusListener() method.
Member variable instance example (Run Applet).
import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MouseListener_GLOBAL_InnerClassDemo extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { private MouseAdapter myMouseListener = new MouseAdapter() { Color oldColor; // the original label color @Override public void mousePressed(MouseEvent e) { oldColor = e.getComponent().getForeground(); e.getComponent().setForeground(Color.yellow); } @Override public void mouseReleased(MouseEvent e) { e.getComponent().setForeground(this.oldColor); } }; public View() { super(); final JLabel label1 = new JLabel("Label 1"); final JLabel label2 = new JLabel("Label 2"); this.add(label1); this.add(label2); label1.addMouseListener(this.myMouseListener); label2.addMouseListener(this.myMouseListener); } } }
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.