0

I have mouseMotionListener in my jpanel code.

But how can I know if the mouse dragged to left or right inside the jpanel?

Dor Cohen
  • 117
  • 1
  • 4
  • 10
  • You'ld need to put previous X and Y coordinates in variables/object, and then compare to the new values. – AntonH Mar 17 '14 at 12:57
  • Have you tried reading [some documentation on it](http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html)?? Or looked at some existing [question](http://stackoverflow.com/questions/15574065/java-mousedragged-and-moving-around-in-a-graphical-interface) (and there's a lot more of them)? – Jonathan Drapeau Mar 17 '14 at 13:03

2 Answers2

0

In the event callback: store the mouse-(x-)position, in the next callback calculate the difference to the previous position (and store the position again); depending on the sign(um) you can determine whether it was a left or right drag.

Tedil
  • 1,935
  • 28
  • 32
0

Use

if (currentX > previousX) {
    // Right
} else {
    // Left
}
previousX = currentX;

in your listener.

Hope this helps.

Tanmay Patil
  • 6,882
  • 2
  • 25
  • 45