Enable blinking of JLabel 3 times and then remain invisible/disappear

拟墨画扇 提交于 2019-12-05 19:10:32

Its very simple, create a sub class below in your JFrame or JDialog.

class LbBlink implements ActionListener {  
        private javax.swing.JLabel label;
        private java.awt.Color cor1 = java.awt.Color.red;
        private java.awt.Color cor2 = java.awt.Color.gray;
        private int count;

        public LbBlink(javax.swing.JLabel label){
            this.label = label;
        }
        @Override
        public void actionPerformed(ActionEvent e) {
            if(count % 2 == 0)
                label.setForeground(cor1);
            else
                label.setForeground(cor2);
            count++;
        }  
    }

Declare a variable in your class.

private Timer timerLB;

After, in your class construct set the variable.

timerLB = new Timer(1000, new "Your Class".LbBlink("Your jLabel"));

Finally, in your application, for start blink

timerLB.start();

And for stop:

timerLB.stop();

try using a counter and increase the counter on every blink and if the counter is 3 then clear the label's text setText("")

EDIT

class TimerListener implements ActionListener{
    int counter = 1;

    public void actionPerformed(ActionEvent evt){
        if(counter == 3){//3 = YOUR MAX
            timer.stop();
        }
        counter++;
    }
}

meaning code lines

  public void setBlinking(boolean flag) {
    this.blinkingOn = flag;
  }

  public boolean getBlinking(boolean flag) {
    return this.blinkingOn;
  }
  • every changes to the (already visible) Swing GUI will be done inside Swing Action, ActionListener in your case

  • there (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.

No timer is need.Simply using THREAD we can done it.

import java.awt.Color;
import javax.swing.*;

class Sample {
   int i;
   private JFrame f;
   private JLabel l;

Sample() throws InterruptedException {
    f = new JFrame("test");
    f.setSize(400, 400);
    l = new JLabel("Testing");
    f.add(l);
    f.setVisible(true);

    for (i = 0; i < 6; i++) {
        if (i % 2 == 0) {
            l.setForeground(Color.red);
        } else {
            l.setForeground(Color.BLUE);
        }
        Thread.sleep(500);
    }
    l.setEnabled(false);

}

public static void main(String a[]) throws InterruptedException {
    new Sample();
}

}

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.

    static int counter = 0;
    static ScheduledExecutorService service;

    void displayHelpMessage(String message) 
    {
        Runnable runnable = new Runnable() {
            public void run() {
                // task to run goes here
                if (counter++ >= 6) {
                    service.shutdown();
                    counter = 0;
                } else {
                    if (counter % 2 == 1) {
                        messageLabel.setForeground(Color.red);
                    } else {
                        messageLabel.setForeground(Color.black);
                    }
                }
            }
        };
        messageLabel.setText(message);
        service = Executors.newSingleThreadScheduledExecutor();
        service.scheduleAtFixedRate(runnable, 0, 750, TimeUnit.MILLISECONDS);
    }   

// Call examples:       
    displayHelpMessage("How to select panel");        
    displayHelpMessage("Key to use complete create/modify");
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!