4-6 Handling Mouse Events

Mouse Interfaces

Java supports a very rich set of methods to handle mouse events, and these are divided into two different interfaces.  The MouseListener interface specifies 5 methods that deal with mouse button clicks and keeping track of when the mouse enters or exits a component.  The MouseMotionListener interface specifies 2 methods that focus on mouse movement.

Here is a summary of the abstract methods in each interface class:

Because these are interface classes, once you agree to implement them in your code you must provide code for all of the methods that they each provide, even if that means you are only providing empty curly braces { .. }.

Time for an example!  The code below demonstrates all 7 of the mouse handling methods specified by the MouseMotionListener and MouseListener interfaces.  To implement more than one interface class you simply separate them with a comma (,) as shown on line 33.

In this example we are using the JFrame default BorderLayout manager, and placing a JPanel widget in the center region, and a status JLabel in the south.

Our inner class for event handling starts on line 33.  Because the MouseEventListener class that we are defining implements both MouseListener and MouseMotion listener we’ll need to implement 7 methods in total.  Those methods follow in the body of the class.

In each case, they update the message in the south statusBar to reflect the event.  Notice also the use of getX() and getY() from the MouseEvent object passed into each method when that event occurs.  Finally, in the case of mouseEntered() and mouseExited() we are also changing the background colour of the JPanel.

Here is a test class:

For convenience, Java 1.5 (and later) include a Swing interface class called MouseInputListener which includes all of the same MouseListener and MouseMotionListener abstract methods (in package javax.swing.event), so you can implement one interface class instead of two if you like.  You would still need to provide code for 7 methods though.

MouseAdapter Class

Let’s say in the example above that we only care about MouseClicked() events.  Even if we only implemented the MouseListener interface this still means we need to define 5 methods, 4 of which will have empty method bodies { .. }.  All of these empty method bodies reduce program readability, and are generally a nuisance to type out all the time!  If only there was a better way…

There is!  For many event listener interfaces that have multiple methods, the Java API provides what are called adapter classes to simplify code.  What an adapter class does is it implements all of the methods of an interface class, each with an empty method body { .. }.  An adapter class is not an interface class, it’s a regular class, so instead of implementing an adapter class you inherit from it.

Think about what this means.  Since we are inheriting from the adapter class (which has already implemented all of an interface class’s methods, providing empty bodies) we only need to override the method(s) that we really need for our event handling.  Very clever!

Let’s see how this works in the case of tracking MouseClicked() events only.

Our inner MouseEventListener class, inherits (extends) MouseAdapter (which has implemented all of the required methods from the MouseListener and MouseMotionListener interface classes).  All we need to do is override the inherited MouseClicked() method with our custom code.  I’m sure you can appreciate how much shorter (and less cluttered) the code is using this technique.

A Java application may be run on a system with a one-, two-, or three-button mouse.  Java provides two methods to distinguish between mouse buttons:

Finally, here’s the test class:

You Try!

  1. Create a GUI that tracks the location of the mouse within a JPanel.  Imagine that the window is divided into 4 quadrants.  As the mouse moves between quadrants, change the background color. Use a different colour for each quadrant.  Your code should work even if the window is resized.  Solve this by implementing the MouseMotionListener interface.
  2. Modify your code so that you inherit from MouseAdapter instead.