Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks


Writing 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 tracking other mouse event types such as clicks, see How to Write a Mouse Listener.

Mouse Motion Event Methods

The MouseMotionListener(in the API reference documentation) interface and its corresponding adapter class, MouseMotionAdapter(in the API reference documentation), contain two methods:
void mouseDragged(MouseEvent)
Called by the AWT in response to the user moving the mouse while holding a mouse button down. This event is fired by the component that fired the preceding mouse-pressed event, even if the cursor is no longer over that component.
void mouseMoved(MouseEvent)
Called by the AWT 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 implementing the mouseDragged and mouseMoved methods instead of the mouse-listener methods. You can find the applet's code in MouseMotionEventDemo.java.


Note: The above applet requires JDK 1.1. If you are using an older browser that does not support 1.1, you won't be able to run the applet. Instead, you need to view this page in a 1.1-compliant browser, such as HotJava or the JDK Applet Viewer (appletviewer).

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. 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, mouse motion event methods use MouseEvent objects. See Writing a Mouse Listener for information about the MouseEvent class.


Previous | Next | Trail Map | Creating a User Interface | Using Components, the GUI Building Blocks