Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a JTree 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?

like image 862
michelemarcon Avatar asked Jul 27 '11 10:07

michelemarcon


1 Answers

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);
}
like image 175
jzd Avatar answered Oct 01 '22 16:10

jzd