Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mouse moved event within JPanel

I'm having some issues with getting my mouse events to work. I have a JPanel inside of a JLayeredPane which is in a JScrollPane. Admittedly, I am fairly new to working with Swing, but essentially, I want the JPanel to react to the mouse moving, but have been unable to get it to work.

public class CellHighlighter extends JPanel implements MouseMotionListener{

    public CellHighlighter(){

    }

    public void mouseMoved(MouseEvent evt){
        System.out.println(evt.getPoint().x + ", " + evt.getPoint().y);
    }

    public void mouseDragged(MouseEvent evt){System.out.println("message");}

}

Any help would be much appreciated, thanks in advance!

like image 798
Shane Fitzgerald Avatar asked Dec 08 '11 23:12

Shane Fitzgerald


People also ask

What are the events of mouse motion listener?

Interface MouseMotionListener The listener object created from that class is then registered with a component using the component's addMouseMotionListener method. A mouse motion event is generated when the mouse is moved or dragged. (Many such events will be generated).

What is mouse exit in Java?

mouseExited(MouseEvent e) Invoked when the mouse exits a component. void. mousePressed(MouseEvent e) Invoked when a mouse button has been pressed on a component.

What is the difference between mouse listener and mouse motion listener?

We can implement a MouseListener interface when the mouse is stable while handling the mouse event whereas we can implement a MouseMotionListener interface when the mouse is in motion while handling the mouse event.

What is use of Mousemotion listener interface?

Introduction. The interfaceMouseMotionListener is used for receiving mouse motion events on a component. The class that process mouse motion events needs to implements this interface.


1 Answers

Are you registering your JPanel Object with the MouseListener? Something like:

    public CellHighlighter(){
       this.addMouseMotionListener(this);
    }

Or maybe you need to add the MouseListener to The ScrollPane or LayeredPane?

like image 72
HectorLector Avatar answered Oct 17 '22 19:10

HectorLector