Reputation: 146
I have found a strange problem while trying to write a drag& drop exercise. It seems, no MouseAdapter.mouseDragged is called while i try to drag a JPanel ( with left click and trying to drag). The code of interest is following: public class DragMouseAdapter extends MouseAdapter{
@Override
public void mouseDragged(MouseEvent e){
System.out.println("Mouse dragged on source: " + e.getSource());
}
}
...
Then, somwthere in JFrame:
DragMouseAdapter my = new DragMouseAdapter();
jPanel1.addMouseListener(my);
jPanel2.addMouseListener(my);
And i see no printout. What is the problem here?
Upvotes: 1
Views: 512
Reputation: 394
mouseDragged
is a part of MouseMotionListener, so you need to use addMouseMotionListener
instead of (or in addition to) addMouseListener
.
Edit: Including the following info in my answer instead of in a comment:
A MouseListener handles f.ex. mouse clicks, while a MouseMotionListener handles mouse motions (dragging). There's also a MouseWheelListener. To register each kind of Listener with a component (in your case, a JPanel), the corresponding methods must be called; addMouseListener, addMouseMotionListener, or addMouseWheelListener. For more info, have a look at How to Write a Mouse-Motion Listener and MouseAdapter API docs
Upvotes: 3
Reputation: 6077
You need a MouseMotionAdapter
instead of MouseAdapter
. Just change your code to:
public class DragMouseAdapter extends MouseMotionAdapter{
@Override
public void mouseDragged(MouseEvent e){
System.out.println("Mouse dragged on source: " + e.getSource());
}
}
Upvotes: 0