Note that there are some explanatory texts on larger screens.

plurals
  1. POJava (Swing): MouseMoved not working at all
    primarykey
    data
    text
    <p>This is driving me nuts. It must be an extremely simple problem, but I can't possibly see it.</p> <p>Basically mouseMoved is NEVER called. Below is the code. </p> <pre><code>public class MouseMotionThing { public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { JFrame jFrame = new JFrame(); jFrame.setContentPane(new ContentPane()); jFrame.setSize(400, 400); jFrame.setVisible(true); jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }); } } </code></pre> <p>And my panel. This implementation uses a MouseInputListener:</p> <pre><code>class ContentPane extends JPanel implements MouseInputListener { int x = 0, y = 0; public ContentPane() { setOpaque(true); addMouseListener(this); addMouseMotionListener(this); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(0, 0, x, y); } @Override public void mouseDragged(MouseEvent e) { System.out.println("Mouse Dragged!"); } @Override public void mouseMoved(MouseEvent e) { System.out.println("CALLED MOUSE MOVED"); x = e.getX(); y = e.getY(); repaint(); } @Override public void mouseClicked(MouseEvent e) { } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } } </code></pre> <p>whereas this alternative implementation uses only a MouseMotionListener:</p> <pre><code>class ContentPane extends JPanel implements MouseMotionListener { int x = 0, y = 0; public ContentPane() { setOpaque(true); addMouseMotionListener(this); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawLine(0, 0, x, y); } @Override public void mouseDragged(MouseEvent e) { System.out.println("Mouse Dragged!"); } @Override public void mouseMoved(MouseEvent e) { System.out.println("CALLED MOUSE MOVED"); x = e.getX(); y = e.getY(); repaint(); } } </code></pre> <p>In none of the above alternative implementations is mouseMoved EVER called. I'm adding the right listeners every single time, but it's simply not working. mouseDragged works fine though. What am I missing?</p> <p>LE: I tested the code on Ubuntu 12.10 &amp; JDK 7, worked fine. Then when I went back to my W8 machine, it started working. I did absolutely nothing more than restart my laptop. I couldn't reproduce the problem nor track it down, but I will come back if I get it again and manage to find something.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload