Java Events using version 1.1.*

Whenever you move your mouse over the Applet area or maybe click a button in your applet (you will learn about buttons later), you create what is called an event. An event is simply the denotation that some particular thing has occurred.... like a button being pressed.

There are three components to Event Handling in Java:

1) Decide which events your applet will handle

  • There is a package java.awt.event that lists all the different kinds of events as Classes! 
    • MouseEvent 
    • KeyEvent 
    • Window Event

2) Listener: Create the code to process the event. We call this a listner.

  • The different listeners are defined as interfaces and are given in java.awt.event
    • MouseListener 
    • KeyListner 
    • WindowListener
       
  • Example MouseListener: 
  • Events Method Defined
    mouse down public void mousePressed(MouseEvent e)
    mouse up public void mouseReleased(MouseEvent e)
    mouse enter public void mouseEntered(MouseEvent e)
    mouse exit public void mouseExited(MouseEvent e)
    mouse clicks public voide mouseClicked(MouseEvent e)

     
  • There are a set of Classes provided that implement each Listner classes with methods that do nothing. They are provided as YOU MUST WRITE YOUR OWN LISTENER CLASS THAT IMPLEMENTS METHODS FROM THE LISTNER INTERFACES 
  • Listner Class
    MouseListner MouseAdapter
    KeyListner KeyAdapter
    WindowListner WindowAdapter
    OPTION 1:  Implementing a Listner Interface

    One option is to create a class, e.g. myMouseListner (or use an existing class you have previously written),  and have it implement the appropriate Listner Interface(e.g. MouseListner)  to handle the events you are interested in.   Then this class's (e.g. myMouseListener) methods which you must implement are used to handle the events of interest.

    • You must implement all of the methods in the interface, even as dummy methods (that do nothing).

  • Example Inside your Applet class  having it implement a Listner Interface. Here we make the applet itself a listener! 
  • import java.awt.event.*;  //need this package
    import java.applet.*;
    
    //Can only have one parent...thus must implement the
    //Event interfaces you are wanting to handle.
    //Thus you must create methods (even if they do nothing)
    // for all of the defined methods in the interface.
    class MyApplet extends Applet implements MouseListener {
    
       public void mousePressed(MouseEvent e) {
           //handle it here
       }
    
       //implement ALL the other mouse*() methods below
       public void mouseClicked(MouseEvent e) {}
       public void mouseReleased(MouseEvent e) {}
       public void mouseEntered(MouseEvent e) {}
       public void mouseExited(MouseEvent e) {}
    }
    OPTION 2:  Extending  a Listner Adapter

    Alternatively, you can create a class (or use an existing class) and have it extend the appropriate adaptor class (e.g. MouseAdaptor). 

    • This way you do not have to implement any methods you do not care about...you simply inherit the default ones from the Adapter class.


     
  • Example You Own Listener Class  extending an Adapter Class.
  • import java.awt.event.*;  //need this package
    
    //will ONLY handle the mouse down event and
    //  do nothing for others
    class MyMouseListner extends MouseAdapter {
    
       public void mousePressed(MouseEvent e) {
           //handle it here
       }
    
    }

3) Register the listener.

This is where you tell the system that a Listener Class whether it is the Applet itself or another seperate class will listen to what events

Done with special methods that you USUALLY call in your Applet's init() method (or the appropriate method in the class containing the GUI components creating the events): 

  • addMouseListener(Object o) 
  • addKeyListener(Object o) 
  • addWindowListener(Object o) 
Object = any java.awt.Component or subclass of it!! 

Example:
Seperate Listener Class
from Class containing GUI
component creating event(s).
Listner Class is same as  
class containing GUI component
creating the event(s).
//Inside the Class containg the GUI component
//e.g. if this is a Applet class place it 
//inside of the init() method
ml = new MyMouseListener();
addMouseListener(ml);
//Inside the Class containg the GUI component
//e.g. if this is a Applet class place it 
//inside of the init() method
 
addMouseListener(this);