0

How to use stop() in java swing Timer class ?

I want to repeat some code with Timer class in java. Here's the code : Timer t = new Timer(1000, new ActionListener() { public void actionPerformed(ActionEvent evt) { // Do action if(workIsDone) { t.stop(); } } } ); t.start(); When i compile it i am getting error: variable t might not have been initialized t.stop(); How to solve this ? I think i am getting this because of anonymous inner class but i am unable to solve it :(

10th Aug 2020, 5:33 PM
Kuber Acharya
Kuber Acharya - avatar
3 Antworten
+ 1
declare t as class member, not local
10th Aug 2020, 7:14 PM
zemiak
+ 1
yes. t declare outside... Am not sure but try Timer.stop(); stop(); simply.. Edit : Kuber Acharya use timer.cancel() instead..
10th Aug 2020, 5:43 PM
Jayakrishna 🇮🇳
0
zemiak Solved! Thank you:)
11th Aug 2020, 3:03 PM
Kuber Acharya
Kuber Acharya - avatar