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.

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

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)

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).

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);
    }

}

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?

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 article has been dead for over six months. Start a new discussion instead.