James Goodwin's code will not work. mousePressed and mouseReleased are both fired from the GUI thread, so blocking in mousePressed will prevent mouseReleased from ever being fired, meaning the loop will continue forever.
If you already have a seperate thread for processing then use mousePressed to indicate to that 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.
Specifically, you should create a TimerTask that does whatever it is you want to do multiple times and queue it using Timer.schedule:
Timer timer = new Timer();
TimerTask task = new MyTimerTask();
private class MyTimerTask extends TimerTask {
public void run() {
// your code here
}
}
public void mousePressed(MouseEvent e) {
timer.scheduleAtFixedRate(task, 0, 1000); // Time is in milliseconds
// The second parameter is delay before the first run
// The third is how often to run it
}
public void mouseReleased(MouseEvent e) {
task.cancel();
// Will not stop execution of task.run() if it is midway
// But will guarantee that after this call it runs no more than one more time
}
I'm pretty sure this is the simplest way, as it doesn't involve faffing around with inter-thread communication.
Oh, and as Peter said, you will have to add code to account for the user mousing down on your app and mousing up somewhere else.