James Goodwin code will not work. mousePressed and mouseReleased both start from the GUI thread, so locking in mousePressed will prevent mouseReleased from starting because the loop will last forever.
If you already have a separate thread for processing, use mousePressed to point to the thread that the event should start, and mouseReleased to stop.
If you don't have a separate thread and donβt want the hassle, a timer is probably the easiest way. javadoc on Timer .
In particular, you must create a TimerTask that does what you want to do several times, and queue it using Timer.schedule:
Timer timer = new Timer(); TimerTask task = new MyTimerTask(); private class MyTimerTask extends TimerTask { public void run() {
I am sure that this is the easiest way, since it is not related to communication between threads.
Oh, and, as Peter said, you will need to add the code for the user account that will run on your application and somewhere else.
source share