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!
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).
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.
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With