MouseListener vs MouseMotionListener

MouseListener vs MouseMotionListener

The java.awt.event package comes with two interfaces to handle the events of mouse – MouseListener and MouseMotionListener. They are differentiated by the state of the mouse (whether the mouse is moving or not moving) when an event generated.

MouseListener vs MouseMotionListener

1. Implement MouseListener when the mouse is stable while handling the mouse event.

2. Implement MouseMotionListener when the mouse is in motion while handling the mouse event.

I. MouseListener Interface – The Stable Mouse Event Handler (of MouseListener vs MouseMotionListener)

This listener interface handles the mouse events when the mouse is itself in stable (not moving) condition. For example, to generate the action of mouse click, the mouse need not be moved.

Out of total 7 possible actions (all are with mouse left button only), an user can perform on mouse, 5 are handled by MouseListener and two are handled by MouseMotionListener.

Note: Here, action means with mouse left button only. Do not think of mouse center button, right button and double click action. They are separate and dealt separately.

The five actions are tabulated.

Sl.No. Action Method of MouseListener that handles
1 Pressing Mouse button void mousePressed(MouseEvent e)
2 Releasing Mouse button (after pressing is over) void mouseReleased(MouseEvent e)
3 Clicking Mouse button (includes two actions of pressing and releasing but with some fixed time gap) void mouseClicked(MouseEvent e)
4 When mouse is just entering a component focus area or window like a frame, MS-Word, MS-Excel etc. void mouseEntered(MouseEvent e)
5 When mouse is just exiting (coming out) a component focus area or window like a frame, MS-Word, MS-Excel etc. void mouseExited(MouseEvent e)

A complete program is available on these methods at Handling MouseEvent with MouseListener Example

II. MouseMotionListener Interface – The Motion Mouse Event Handler (of MouseListener vs MouseMotionListener)

This listener interface handles the mouse events when the mouse is in moving (in motion) state. For example, to select some text in MS-Word, it is required to move the mouse. Here comes MouseMotionListener to handle the mouse generated events.

The two possible actions include moving the mouse while being left mouse button is pressed (known as "mouse dragged") or simply just move the mouse (just catching the mouse and move without touching any mouse buttons) over a component or a window (infact, window is also a component) and this action is known as "move moved".

The two actions are tabulated.

Sl.No. Action Method of MouseListener that handles
1 Pressing the left mouse button and at the same time moving the mouse void mouseDragged(MouseEvent e)
2 Just moving the mouse without using any mouse buttons void mouseMoved(MouseEvent e)

A complete program is available on these methods at Handling MouseEvent with MouseMotionListener Java Example

Leave a Reply

Your email address will not be published. Required fields are marked *