javaswingmousemotionlistener

A central MouseMotionListener in java


I have added a MouseMotionListener in my JFrame to control all mouse motion messages that arrived from all objects in my jframe, but when I move my mouse over a JLayeredPane no message is produced. Please help me to add a central MouseMotionListener in my JFrame to control all messages from all objects in it.

great thanks.


Solution

  • You would want to use a AWTEventListener to listen for all AWT messages.

    Following code shows how to listen for mouse and key events:

    long eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK
        + AWTEvent.MOUSE_EVENT_MASK
        + AWTEvent.KEY_EVENT_MASK;
    
    Toolkit.getDefaultToolkit().addAWTEventListener( new AWTEventListener()
    {
        public void eventDispatched(AWTEvent e)
        {
            System.out.println(e.getID());
        }
    }, eventMask);
    

    See Global Event Listeners for more information.