I have View
with some components handles. I can change color of label like this.
labelHandle.setForeground(Color.Blue);
I want to make text of this label
blinking - the idea is that on the View
I will have method blink(Boolean on)
and if I call blink(true)
the method will start new thread which will be class implementing Runnable
.
loop of run()
will be something like this
run() {
for(;;) {
if(finished == true) break;
Thread.sleep(300);
//this will do the blinking
if(finished == true) break; //if durring sleep finish was called => end
if(change) labelHandle.setForeground(Color.Blue);
else labelHandle.setForeground(Color.Black);
change = (change) ? false : true; //switch the condition
}
}
There will be some important points:
boolean finished - says if the thread is finished, the loop for bliniking
void fihish() {this.finished = false;} //stops the thread job
boolean change - switch for the collors, each end of loop switch this value, if its true set one color, if its false sets another => here we have blinking ...
Until here its pretty clear how to do it. The main idea is make thread which will carry about the blinking.
In view I will call:
blink(true);
blink(false);
which will stars and stops the thread.
At one time only one view can be active, so I want have only one thread for blinking, which can be used with more views. The thread will be class implementing Runnable
and should be Singleton
to ensure, there will be only one thread.
Is it good idea to make Singleton
thread like this? (Good or bad practice)