1

I'm trying to get the mouse position while pressing the mouse button but it doesn't work. I'm extending the MouseAdapter and as stated at the Javadoc the mouseMove() is invoked when the mouse cursor has been moved onto a component but no buttons have been pushed.

This is an example class I have created to show you my problem:

public class TestMouse extends MouseAdapter{

    int x,y;
    boolean pressed;

    public void mousePressed(MouseEvent e){
        pressed = true;
    }

    public void mouseReleased(MouseEvent e){
        pressed = false;
    }

    /*
    Invoked when the mouse is not pressed only.
    */
    public void mouseMoved(MouseEvent e){
        x = e.getX();
        y = e.getY();
    }   


    /*
    I want something like that.
    */
    public void mousePressedAndMoved(MouseEvent e){
        ....
    }

}
1
  • Have you tried mouseDragged() ?
    – arcy
    Nov 15, 2013 at 13:36

1 Answer 1

3

That's the problem with MouseAdapter, since it's a abstract class and not an interface (MouseMotionListener is the one you need), it gives empty implementations for all the possible events just to avoid you from being forced to override them all, this also implies that you could miss some of these events if you don't read docs.

If you look carefully at documentation though, you will see that you have

public void mouseDragged(MouseEvent e)

that you can override to listen exactly to what you need.

1
  • What else you need? You are already overriding mouseMoved, that's the same, just called in a different situation.
    – Jack
    Nov 15, 2013 at 13:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.