Writing Event Listeners |
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
TheMouseMotionListener
interface and its corresponding adapter class,MouseMotionAdapter
, 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 substitutingMouseMotionListener
forMouseListener
, and implements themouseDragged
andmouseMoved
methods instead of the mouse listener methods. You can find the applet's code inMouseMotionEventDemo.java
andBlankArea.java
.
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:
- Move the cursor into the yellow rectangle at the top of the applet.
You'll see one or more mouse-moved events.- 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 themousePressed
method (fromMouseListener
),mouseDragged
(fromMouseMotionListener
),mouseReleased
(fromMouseListener
). Thus, this class must implement bothMouseListener
andMouseMotionListener
. To avoid having to define too many empty methods, this class doesn't implementMouseListener
directly. Instead, it extends MouseAdapter and implements onlyMouseMotionListener
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 calledMouseMotionEvent
! Instead, each mouse-motion event method has aMouseEvent
argument. See The MouseEvent Class for information about usingMouseEvent
objects.
Writing Event Listeners |