Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>If you don't have a separate thread and don't want the hassle, a Timer is probably the easiest way. <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Timer.html" rel="noreferrer" title="javadoc on Timer">javadoc on Timer</a>.</p> <p>Specifically, you should create a TimerTask that does whatever it is you want to do multiple times and queue it using Timer.schedule:</p> <pre><code>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 } </code></pre> <p>I'm pretty sure this is the simplest way, as it doesn't involve faffing around with inter-thread communication.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload