-1

I have invoked cancel() method of Timer class outside the run() method of TimerTask class.

I want to know is it possible to restart this timer?

On stop button I have invoked cancel() method :-

private void stopBtnActionPerformed(java.awt.event.ActionEvent evt) {                                        
t.cancel();}

Here t is instance of Timer class.

  • 3
    ..how is this connected with Swing? For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example). – Andrew Thompson Mar 19 '14 at 09:59
  • See http://stackoverflow.com/questions/9547267/unable-to-start-the-timer-in-a-service-in-android – hmjd Mar 19 '14 at 10:06
  • u can possibly use same TimerTask with a new instance of Timer –  Mar 19 '14 at 10:13
  • 2
    you probably want to use javax.swing.Timer (vs. java.util.Timer) – kleopatra Mar 19 '14 at 10:18
  • 2
    Why did you post an uncompilable code snippet? Again.. *For better help sooner, post a [MCTaRE](http://stackoverflow.com/help/mcve) (Minimal Complete Tested and Readable Example).* – Andrew Thompson Mar 19 '14 at 10:27

1 Answers1

1

You can't restart a Timer once cancelled - My advice would be to create a new one. Obviously you could wrap this in your own restartable Timer implementation that merely creates a new Timer under the covers each time.

Adamski
  • 54,009
  • 15
  • 113
  • 152