Handling Mouse Events with MouseAdapter Example

1. What is MouseListener?

We know, java.awt.event package comes with many event classes and listener interfaces to handle the events raised by AWT components like frame or button etc (known as semantic events). But the same package also comes with listeners that can handle the events of hardware components, known as low-level events. Two listeners exist to handle the low-level events raised by mouse – MouseListener and MouseMotionListener. One more low-level event handler is KeyListener (or KeyAdapter). These we see later.

MouseListener handles the events of a mouse when the mouse is stable (not in motion; for motion, use MouseMotionListener).

2. What is the headache with listener interfaces?

Listener interfaces, one way, are very tedious to write the code as all the abstract methods should be overridden even not required by the Programmer. The best example is WindowListener. This interface comes with 7 abstract methods which should be overridden to close the frame. Infact, only one abstract method out of 7 is used to close (windowClosing(WindowEvent)). All the remaining methods are given empty bodies to compile with the interface contract.

3. Then, what is MouseAdapter?

To overcome the above headache, JDK 1.1 comes with a new concept called adapters. Adapters are abstract classes that replace the listeners. Using adapter, what ever abstract method is required, only that can be overridden and the others simple ignored. Any listener comes with more than one abstract method has got a corresponding adapter class. Adapters are very clearly discussed in Java AWT Adapters.

For example, MouseAdapter (instead of MouseListener) permits to override only mouseClicked() instead forcing all abstract methods to override.

MouseAdapter Example. Observe two classes involved and their integration (linking two classes)
import java.awt.*;
import java.awt.event.*;

public class FrameMouseAdapterExample extends Frame
  public FrameMouseAdapterExample()
   		                         // adding MouseAdapter to frame
    PleaseHandle ph = new PleaseHandle();

    setSize(300, 300);

  public static void main(String args[])
    new FrameMouseAdapterExample();

class PleaseHandle extends MouseAdapter  // second class             
  public void mouseClicked(MouseEvent e)
    System.out.println("You clicked mouse at coordinates " + e.getX() + ", " + e.getY());

class PleaseHandle extends MouseAdapter

As MouseAdapter is an abstract class, we cannot implement it. Already the main program FrameMouseAdapterExample is extending Frame. As Java does not support multiple inheritance, MouseAdapter cannot be extended further. To overcome this problem, a separate class of our own PleaseHandle is created extending MouseAdapter.

Now one more problem arises. How to link the two separate classes (having .class files separately) FrameMouseAdapterExample (having frame) and PleaseHandle (having MouseAdapter) where both are not involved in inheritance? See next.

PleaseHandle ph = new PleaseHandle();

addMouseListener() is a method of frame with which MouseListener can be linked to frame. The general way of writing is addMouseListener(this). To overcome the above problem, the linking method is little bit changed. An object of PleaseHandle is created and passed to addMouseListener() method. Now everything works fine.

The above two statements can be modified into a single statement as follows.

addMouseListener(new PleaseHandle());

Every programmer writes like this only. Still simple way is there using anonymous inner classes.

Observe, in PleaseHandle class overrides only mouseClicked() method but not the remaining four. This is the advantage of MouseAdapter over MouseListener.

MouseAdapter Example
Output screen at DOS prompt of MouseAdapter Example

Leave a Comment

Your email address will not be published.