Squareoot
Squareoot

Reputation: 280

Java MouseListener disables MouseMotionListener when mouse pressed

Inside the constructor:

   addMouseListener(new MouseAdapter() {

       public void mousePressed(MouseEvent e){
           //Do something
       }
       public void mouseReleased(MouseEvent e){
           //Do something
       }
   });
   addMouseMotionListener(new MouseMotionAdapter(){
       public void mouseMoved(MouseEvent evt) {
           cursorX = evt.getX();
           cursorY = evt.getY();
       }
   });

mouseMoved is running while I don't click / Press any mouse button.

But when I click or hold the mouse button the cursor position is no longer updated and mouseMoved doesn't get called

I was searching for a solution for hours please help me!

I tried to implement MouseListener and MouseMotionListener to the class but this didn't work too.

Upvotes: 0

Views: 1157

Answers (2)

MadProgrammer
MadProgrammer

Reputation: 347194

The simple answer is, there's a difference between the mouse been moved across the surface of your component when the button is held and when it's not. The system will identify these differences and call different functionality.

This is done by design, presumably to make it easier to manage the two scenarios, which can generate different results (i.e. Drag'n'Drop)

The following example is a simple demonstration. When you press the mouse button and move the mouse, mouseDragged will be called, otherwise mouseMoved will be called

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        public TestPane() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent e) {
                    System.out.println("Pressed");
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    System.out.println("Released");
                }
            });

            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseMoved(MouseEvent e) {
                    System.out.println("Moved");
                }

                @Override
                public void mouseDragged(MouseEvent e) {
                    System.out.println("Mouse Dragged");
                }               
            });
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 200);
        }

    }

}

Upvotes: 3

Taarak
Taarak

Reputation: 48

I don't know why is this happening to you, but you can use the same code inside mouseMoved(MouseEvent evt) inside mouseDragged(MouseEvent evt) of MouseMotionListener too, that is triggered when the mouse is moving and pressing at the same time.

If this not work, this means that MouseListener are taking priority over MouseMotionListener, and in this case I don't know what you can do.

Upvotes: 1

Related Questions