1

I'm using MouseListeners and I want it to tell me the mouse position without th mouse being clicked or held down. So I know where it is hovering over. How do I do this?

I have code such as this, to find the position when it is pressed but i'm unsure how to find the position all the time.

  public void mousePressed(MouseEvent me) {
    print("Mouse Pressed");
      xPosClicked = me.getX() ;
      yPosClicked = me.getY() ;
      print("(" + xPos + "," + yPos + ")");
  }
raffian
  • 31,267
  • 26
  • 103
  • 174
Michael Haywood
  • 319
  • 2
  • 4
  • 10

2 Answers2

4

Use a MouseMotionListener to listen for movement: http://docs.oracle.com/javase/tutorial/uiswing/events/mousemotionlistener.html

Xabster
  • 3,710
  • 15
  • 21
1

Use the mouse motion listener

jPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
        public void mouseMoved(java.awt.event.MouseEvent evt) {

        }
    });
JWqvist
  • 717
  • 6
  • 15