0

How can I get the mouse position even when an event hasn't been performed, eg. I haven't clicked or left window, etc. Kind of like mousing over something. I am currently using a class that implements MouseListener, MouseMotionListener for events such as clicking and releasing but I would like a mouseover ability as well.

2
Contributors
11
Replies
33
Views
4 Years
Discussion Span
Last Post by JamesCherrill
Featured Replies
  • Yes. Use MouseEntered/MouseExited. If you want more detail while its moving inside the window use mouseMoved. Just try it! Read More

0

But within the window? Eg. if my mouse is over this part of my window.

1

Yes. Use MouseEntered/MouseExited. If you want more detail while its moving inside the window use mouseMoved.
Just try it!

0

It's not working, it recognizes when it enters/leaves the screen but it doesn't update the mouse position when I move the mouse in the window. What do I do/How do I implement it correctly? (assuming I am doing it wrong)

0

Nope. In my class that implements MouseListener and MouseMotionListener, I put a System.out.println in the MouseMoved but it never prints. It will print things like 'exited' (placed in the mouseExited method).

0
package main;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

public class mous extends player implements MouseListener, MouseMotionListener
{
    int mouseclicked, mousereleased;
    int x0, y0, xf, yf;
    public void mouseDragged(MouseEvent arg0) 
    {
        //System.out.println("dragged");
    }
    public void mouseMoved(MouseEvent arg0)
    {
        //System.out.println("moved");
        xf=arg0.getX();
        yf=arg0.getY();
        System.out.println("MOVED");
    }
    public void mouseClicked(MouseEvent arg0)
    {
        //System.out.println("clicked");
        mouseclicked=1;
        x0=arg0.getX();
        y0=arg0.getY();
    }
    public void mouseEntered(MouseEvent arg0)
    {
        System.out.println("entered");
    }
    public void mouseExited(MouseEvent arg0) 
    {
        //System.out.println("exited");
    }
    public void mousePressed(MouseEvent arg0) 
    {
        //System.out.println("pressed");
        x0=arg0.getX();
        y0=arg0.getY();
        //System.out.println(x0+":"+y0);
    }
    public void mouseReleased(MouseEvent arg0) 
    {
        //System.out.println("released");
        xf=arg0.getX();
        yf=arg0.getY();
        mousereleased=1;
        //System.out.println(xf+"|"+yf);
    }

}
0

I'm baffled! That looks OK, and if you mistyped it then it wouldn't even compile.
I have loads of code that uses mouseMoved, and I've never had a problem with it.
Sorry.
Maybe someone else has an idea?

ps: I assume you are calling both addMouseListener and addMouseMotionListener?

0

There it is. I din't call on addMouseMotionListener. Thanks!

0

Aha - no such thing as a stupid question! I bet you're not the first person to do that.
Please mark this "solved" for our knowledge base.
Have a good evening
J

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.