I intend on writing java code which controls a JLabel to blink three times and then after the third blink enable the text within it to remain transparent/"disappear."
As indicated from the code below, I've been able to write a JLabel which continuously blinks but would like to create one that blinks only three times and then enable the text within it to remain transparent.
public class BlinkLabel extends JLabel {
private static final long serialVersionUID = 1L;
private static final int BLINKING_RATE = 1000; // in ms
private boolean blinkingOn = true;
public Timer timer;
public BlinkLabel(String text) {
super(text);
timer = new Timer( BLINKING_RATE , new TimerListenerTwo());
timer.setInitialDelay(0);
timer.start();
}
public void setBlinking(boolean flag) {
this.blinkingOn = flag;
}
public boolean getBlinking(boolean flag) {
return this.blinkingOn;
}
public class TimerListenerTwo implements ActionListener{
int counter = 1;
public TimerListenerTwo() {
}
public void actionPerformed(ActionEvent evt){
if(counter == 3){//3 = YOUR MAX
timer.stop();
}
counter++;
}
}
}
I call the above function as follows:
BlinkLabel label = new BlinkLabel("");
label.setText("Blink blink");
How can I edit my above code to enable the JLabel to blink three time and have the text disappear.
Any ideas/suggestions are greatly appreciated.
Its very simple, create a sub class below in your JFrame or JDialog.
Declare a variable in your class.
After, in your class construct set the variable.
Finally, in your application, for start blink
And for stop:
ActionListener
, Swing Action has implemented isEnabled()meaning code lines
every changes to the (already visible) Swing GUI will be done inside
Swing Action
,ActionListener
in your casethere (maybe) not reason to subclassing JLabel, create an local variable for
JLabel
,Swing Timer
,I miss there code
Timer.setRepeats(boolean)
Count blinks and when it is enough call stop on your timer.
Someone suggested using displayHelpMessage (Reference: https://blog.ajduke.in/2014/03/31/java-how-to-schedule-a-task-to-run-in-an-interval/)
So, I used it and developed Java code that blinks given text 3 times on a JLabel(messageLabel) below. Hope this will help somebody out there.
No timer is need.Simply using THREAD we can done it.
}
try using a
counter
and increase thecounter
on everyblink
and if the counter is 3 then clear the label's textsetText("")
EDIT