开发者

How to programmatically fire a MouseEvent to a MouseListener with Java?

开发者 https://www.devze.com 2023-03-23 18:43 出处:网络
I have a JTre开发者_Go百科e with a custom associated MouseListener (for showing popup etc.). I need to fire a MouseEvent that will be caught by the MouseListener. How should I do that programmatically

I have a JTre开发者_Go百科e with a custom associated MouseListener (for showing popup etc.). I need to fire a MouseEvent that will be caught by the MouseListener. How should I do that programmatically?


You could create your own MouseEvent and loop through all the listeners and make the call.

For example:

MouseEvent me = new MouseEvent(tree, 0, 0, 0, 100, 100, 1, false);
for(MouseListener ml: tree.getMouseListeners()){
    ml.mousePressed(me);
}


The Robot class might be what you're looking for.

This class is used to generate native system input events for the purposes of test automation, self-running demos, and other applications where control of the mouse and keyboard is needed. The primary purpose of Robot is to facilitate automated testing of Java platform implementations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号