how to know if mouse dragged left or right inside jpanel in java

746 views Asked by At

I have mouseMotionListener in my jpanel code.

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

2

There are 2 answers

1
Tanmay Patil On BEST ANSWER

Use

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

in your listener.

Hope this helps.

0
Tedil On

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.