0

I have the following code. I want to Make jLabel Visible and Invisible 4 times with 0.5Seconds delay for every loop. The code below is not working as expected.

String a = (String)jTextField1.getText();

  if(a.equals("")){

  for (int i = 0;i<=3;i++){
      try {
         jLabel1.setVisible(true);
          Thread.sleep(500);


          for(int j=0;j<i;j++){
          jLabel1.setVisible(false);
           Thread.sleep(500);
          }

      } catch (InterruptedException ex) {

      }
  }
  }
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Asif Ameer
  • 85
  • 1
  • 2
  • 14
  • 2
    Short answer, you don't. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for reasons why and [How to use Swing Timers](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) for a possible solution – MadProgrammer Feb 26 '16 at 05:16
  • In general for timers you could use http://www.journaldev.com/1050/java-timer-and-timertask-example-tutorial . Java Timer class can be used to schedule a task to be run one-time or to be **run at regular intervals**. For swing use Swing's Timer. – Developer Marius Žilėnas Feb 26 '16 at 05:16
  • @Willmore `java.util.Timer` really isn't appropriate for this problem, as you still need to take into consideration the non-thread safe nature of Swing, instead a Swing `Timer` would be a more appropriate solution – MadProgrammer Feb 26 '16 at 05:18
  • @MadProgrammer thank you :) – Developer Marius Žilėnas Feb 26 '16 at 05:19
  • you try this link ..http://stackoverflow.com/questions/16596428/removing-a-jlabel-after-10-seconds – Kumaresan Perumal Feb 26 '16 at 05:19

0 Answers0