javadrag-and-dropmouseeventmousemousemotionlistener

Switching drag events between panel with MouseMotionListener - Java


I've got a problem with MouseMotionListener interface. I want to start dragging from a button component, and I want to stop it in another area of the program,that is in another panel. The problem is that the coordinates of the mouse are negative, because they have as origin the component where the event has started. So I was wondering, how could I get back the coordinates referring to the component the mouse is on instead of the starting component?

Thanks in advance for any help.


Solution

  • I managed to get the coordinates relative to the component where the mouse is on getting the absolute coordinates of the mouse and then subtracting the absolute location of the component in the screen:

    Point point = MouseInfo.getPointerInfo().getLocation();
    point.x -= currentComponent.getLocationOnScreen().x;
    point.y -= currentComponent.getLocationOnScreen().y;