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.

Recommended Answers

All 11 Replies

MouseListener has methods for MouseEntered/MouseExited that you can use for mouseover handling.

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

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

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)

You should be getting many calls to your mouseMoved method as part of ypur MouseMotionListener

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

is it possible you mistyped the name of the method? Can you post the relevant code?

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?

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.