Dispatch MouseEvent

旧街凉风 提交于 2019-12-07 03:43:39

问题


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


回答1:


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);



回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/2708408/dispatch-mouseevent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!