MichaelG MichaelG - 6 months ago 10
Java Question

What is the best way to check the running status of a Timer?

Desired Output: I would like to check for the running condition of the timer and run some code based on that status.

I have the following code...

if (spin2 == spin3 && timer1.stop()==true)
{

lblMessage.setText("Congratulations. You matched 2 symbols");
credit = credit + 50;
lblCredit2.setText(credit + " ");
lblCredit2.setForeground(new Color(11,212,0));
timer1.stop();
timer2.stop();
timer3.stop();

}


However, timer1.stop() == true does not work. I need an alternative...

An Idea:

Is changing the value of a boolean when I call start() or stop() the best way to approach the running condition of a timer? or Is there another solution?

Answer

Solution:

Instead of

timer1.stop()==true //Which isnt even valid because timer.stop doesnt return a boolean

Use

timer1.isRunning(); //It returns a boolean (true or false) if the timer is running or not.

Returns True when timer is Running.

Returns False when timer is not Running.


This would have been a simple fix if you refered to the API.

Heres the Link:

https://docs.oracle.com/javase/7/docs/api/javax/swing/Timer.html#isRunning()

Comments