Why are mouseDragged-events not received when using MouseAdapter?

you gotta add your MouseAdapter as both mouseListener and mouseMotionListener, and you'll be golden. MouseAdapter implements both MouseListener and MouseMotionListener, but your component doesn't know to pass mouseDragged events to it unless you call addMouseMotionListener


If you add it through

this.addMouseListener(new mouseEventHandler()); 

you will not receive motion related MouseEvents (That's not what you registered the listener for!)

You'll have to add the listener twice, i.e., add it using addMouseMotionListener as well:

mouseEventHandler handler = new mouseEventHandler();
this.addMouseListener(handler); 
this.addMouseMotionListener(handler);

in order to get both type of events.

(A side node, always use a capital first letter for your classes, i.e., use MouseEventHandler instead :-)


The top answers for this question are now pretty old. For anyone using Java JDK 8 or later, be sure to check out https://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html.

To summarize, your listener class has to extend from MouseInputAdapter instead of MouseMotionAdapter or MouseAdapter. You'll add your listener class using addMouseMotionListener and addMouseListener as follows:

MyMouseHandler myMouseHandler = new MyMouseHandler ();
addMouseMotionListener(myMouseHandler);
addMouseListener(myMouseHandler);