0

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.

user250327
  • 33
  • 1
  • 5

2 Answers2

1

Use the method MouseInfo.getPointerInfo().getLocation() - it returns a Point object corresponding to current mouse position.

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