How to programmatically launch MouseEvent in MouseListener with Java?

I have a JTree with a custom MouseListener (to display a popup, etc.). I need to run MouseEvent , which will be caught by MouseListener . How to do this programmatically?

+6
source share
2 answers

You can create your own MouseEvent and skip all listeners and make a call.

For instance:

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

The Robot class may be what you are looking for.

This class is used to generate your own system input events in order to automate testing, self-running demos and other applications that require mouse and keyboard control. Robot's main goal is to facilitate automated testing of Java platform implementations.

+3
source

Source: https://habr.com/ru/post/893673/


All Articles