Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are mouseDragged-events not received when using MouseAdapter?

Why are mouseDragged-events only received when using MouseMotionAdapter
and not when using MouseAdapter ?

Java has two abstract adapter classes for receiving mouse-events ;
MouseAdapter and MouseMotionAdapter.

Both classes have a mouseDragged(MouseEvent e)-method, but the
one in MouseAdapter does not seem to work ; mouseDragged-events
never get through with this one.

Both classes implement the MouseMotionListener-interface which
defines the mouseDragged-event, so I don't understand why it is
not working correctly on both of them.

Here is sample-code which shows this issue :

import java.awt.event.MouseAdapter;  
import java.awt.event.MouseEvent;  
import java.awt.event.MouseMotionAdapter;  
import javax.swing.JFrame;  

public class SwingApp extends JFrame
{
    public SwingApp()
    {   
        // No mouseDragged-event is received when using this : 
    this.addMouseListener(new mouseEventHandler()); 

    // This works correct (when uncommented, of course) :
    // this.addMouseMotionListener(new mouseMovedEventHandler());


    setBounds(400,200, 550,300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setResizable(false);
    setVisible(true);
    }   

  public static void main(String args[])
  {
    new SwingApp();
  }

  class mouseEventHandler extends MouseAdapter
  {         
    @Override
    public void mouseDragged(MouseEvent e) // Why is this method never called ?
    {
        System.out.println(String.format("MouseDragged via MouseAdapter / X,Y : %s,%s ", e.getX(), e.getY()));
    }
  } 

  class mouseMovedEventHandler extends MouseMotionAdapter
  {           
    @Override
    public void mouseDragged(MouseEvent e)
    {
        System.out.println(String.format("MouseDragged via MouseMotionAdapter / X,Y : %s,%s ", e.getX(), e.getY()));
    }
  } 

}
like image 515
Tim Avatar asked Apr 07 '11 07:04

Tim


3 Answers

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 :-)

like image 72
aioobe Avatar answered Sep 22 '22 14:09

aioobe


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

like image 31
iluxa Avatar answered Sep 21 '22 14:09

iluxa


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);
like image 37
jvn91173 Avatar answered Sep 23 '22 14:09

jvn91173