Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Writing Event Listeners

How to Write a Mouse-Motion Listener

Mouse-motion events tell you when the user uses the mouse (or a similar input device) to move the onscreen cursor. For information on listening for other kinds of mouse events, such as clicks, see How to Write a Mouse Listener.

[PENDING: mention swing's MouseInputAdapter dodad]

Mouse-Motion Event Methods

The MouseMotionListener(in the API reference documentation) interface and its corresponding adapter class, MouseMotionAdapter(in the API reference documentation), contain these methods:
void mouseDragged(MouseEvent)
Called in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the most recent mouse-pressed event, even if the cursor is no longer over that component.
void mouseMoved(MouseEvent)
Called in response to the user moving the mouse with no mouse buttons pressed. This event is fired by the component that's currently under the cursor.

Examples of Handling Mouse-Motion Events

The following applet contains a mouse-motion listener. It's exactly like the applet in How to Write a Mouse Listener, except for substituting MouseMotionListener for MouseListener, and implements the mouseDragged and mouseMoved methods instead of the mouse listener methods. You can find the applet's code in MouseMotionEventDemo.java and BlankArea.java.

Click this figure to run the applet.
This is a picture of the applet's GUI. To run the applet, click the picture. The applet will appear in a new browser window.


Try this:
  1. Move the cursor into the yellow rectangle at the top of the applet.
    You'll see one or more mouse-moved events.
  2. Press and hold the mouse button, and then move the mouse so that the cursor is outside the yellow rectangle.
    You'll see mouse-dragged events.

The following code is from an event handling class in the RectangleDemo.java source file. [PENDING: doublecheck code. should this demo use Swing's MouseInputAdapter? update the demo code to be swing instead of AWT] This class handles three kinds of events: mouse presses, mouse drags, and mouse releases. These events correspond to the mousePressed method (from MouseListener), mouseDragged (from MouseMotionListener), mouseReleased (from MouseListener). Thus, this class must implement both MouseListener and MouseMotionListener. To avoid having to define too many empty methods, this class doesn't implement MouseListener directly. Instead, it extends MouseAdapter and implements only MouseMotionListener directly.

...//where initialization occurs:
    MyListener myListener = new MyListener();
    addMouseListener(myListener);
    addMouseMotionListener(myListener);
...
class MyListener extends MouseAdapter 
                 implements MouseMotionListener {
    public void mousePressed(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect = new Rectangle(x, y, 0, 0);
        repaint();
    }

    public void mouseDragged(MouseEvent e) {
        updateSize(e);
    }

    public void mouseMoved(MouseEvent e) {
        //Do nothing.
    }

    public void mouseReleased(MouseEvent e) {
        updateSize(e);
    }

    void updateSize(MouseEvent e) {
        int x = e.getX();
        int y = e.getY();
        currentRect.setSize(x - currentRect.x,
                            y - currentRect.y);
        repaint();
    }
}

Event Methods Used by Mouse-Motion Listeners

Each mouse-motion event method has a single parameter -- and it's not called MouseMotionEvent! Instead, each mouse-motion event method has a MouseEvent argument. See The MouseEvent Class for information about using MouseEvent objects.


Previous | Next | Trail Map | Creating a GUI with JFC/Swing | Writing Event Listeners