How to set 5sec timer for blinking border?

142 Views Asked by At

Using javax.swing.Timer, I tried to make a blinking border of TextField. My problem is, this runs continuously. I only need it to blink for like 5 seconds. How can stop it after 5secs? Or is there any other way to make this blinking simple? I got stuck in this :( Thanks in advance for help!

 public static void borderFlash () {
                
             Action flashBorder = new AbstractAction () {
             boolean hasBorder = true;
             public void actionPerformed (ActionEvent e) {
             EmptyBorder eb = new EmptyBorder(5, 5, 5, 5);
             MatteBorder mb = new MatteBorder(5, 5, 5, 5, Color.BLUE);
                  if (hasBorder)
                    queue1.setBorder(eb);
                  else
                    queue1.setBorder(mb);
                  hasBorder = !hasBorder;
                }
              };
              
              Timer t = new Timer(200, flashBorder);
                      t.start();        
  }
1

There are 1 best solutions below

1
On

I would suggest:

  1. Once you are using Timer you can apply a TimerTask. See: https://www.baeldung.com/java-timer-and-timertask

  2. Is to use Thread.sleep()

I hope that if it not solve your problem, at least give you some ideas to work with