Search code examples
javajpanelmousemotionevent

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


I have mouseMotionListener in my jpanel code.

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


Solution

  • Use

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

    in your listener.

    Hope this helps.