As the title says I want to know where my program to know where my mouse is while I'm holding a mouse button and moving it around the JFrame. I could only find a way to detect after I stop holding the button.
Asked
Active
Viewed 147 times
2 Answers
1
Use the method MouseInfo.getPointerInfo().getLocation() - it returns a Point object corresponding to current mouse position.

Ricardo Meneghin Filho
- 414
- 3
- 13
-
Exactly what I needed. Thank you very much. – user250327 Jul 25 '15 at 02:03
0
Add a MouseEventListener to your Button or JFrame. The based on event either MousePress or MouseClick, you can get position event.getX() or event.getY()
public class MouseEventDemo implements MouseListener {
//Register for mouse events on button or frame
yourButton.addMouseListener(this);
...
public void mousePressed(MouseEvent e) {
saySomething("Mouse pressed; # of clicks: " + e.getX(), e);
}
}

Utkarsh
- 589
- 3
- 19