6

Is there a way to dispatch MouseEvent, same as dispatchKeyEvent using the KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(listener); that happens before the event transferred to the component ?

I know I have 2 options

1) add mouse event to all compoenents recursive

2) use a transparent glasspane

Does Java support this , or do I have to use the one of the options above?

thank you

Ben
  • 54,723
  • 49
  • 178
  • 224
shay
  • 1,317
  • 4
  • 23
  • 35
  • Can the java.awt.Robot class help you ? – Romain Hippeau May 02 '10 at 20:37
  • i didn't come to think of that , this is totally cool come to think about it :) let me check :) – shay May 05 '10 at 19:11
  • java.awt.Robot will give me the ability to move mouse but i don't see any way to register MouseListener to it http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Robot.html – shay May 05 '10 at 20:16

3 Answers3

10

Have you tried java.awt.Component.dispatchEvent(AWTEvent)?

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

JButton jb = new JButton("Press!");
MouseEvent me = new MouseEvent(jb, // which
    MouseEvent.MOUSE_CLICKED, // what
    System.currentTimeMillis(), // when
    0, // no modifiers
    10, 10, // where: at (10, 10}
    1, // only 1 click 
    false); // not a popup trigger

jb.dispatchEvent(me);
shemnon
  • 5,318
  • 4
  • 28
  • 37
3

what i finally did was

long  eventMask = AWTEvent.MOUSE_MOTION_EVENT_MASK
         + AWTEvent.MOUSE_EVENT_MASK;
Toolkit.getDefaultToolkit().addAWTEventListener(
         new MouseListener(){....}, eventMask);

thank you alll

mKorbel
  • 109,525
  • 20
  • 134
  • 319
shay
  • 1,317
  • 4
  • 23
  • 35
1

I've finally used this:

        a.dispatchEvent(new MouseEvent(a,
                               MouseEvent.MOUSE_MOVED,
                               System.currentTimeMillis() + 10,
                               MouseEvent.NOBUTTON,
                              x,y,
                               0,
                               false));

Some explanation for params: X Mouse X to move Y Mouse Y to move A is the component
I hope i have been helpfull for people with the same question.