Skip to Main Content

What should I use instead of thread.sleep on a swing applet?

DynamiAug 22 2017 — edited Aug 24 2017

Hello.

I was trying to make a Timer, where you set the time with a jComboBox, you press a toggle button and it starts.

But, when I made a nested for loop with a 1 sec delay on each loop, thread.sleep froze the aplication.

Then I tried the swing timer. But I can't use it because the Timer needs to be in a text box, and jTextBox does not implement the ActionListener Interface.

What should I do?

And, I feel like I am asking questions too frequently. Should I go back to the basics?

Thanks in advance.

Code at the moment :

    private void jTextField1ActionPerformed(java.awt.event.ActionEvent evt) {                                           

        String temp1 =(String) jComboBox3.getSelectedItem();

        String temp2 =(String) jComboBox2.getSelectedItem();

        String temp3 = (String) jComboBox1.getSelectedItem();

        int hour =Integer.parseInt(temp1);

        int min = Integer.parseInt(temp2);

        int sec = Integer.parseInt(temp3);

               

        int startHour = hour;

        int startMin = min;

        int startSec = sec;

      if("Timer is OFF".equals(jToggleButton1.getText())){

       

    } else{ {

          for (hour = startHour; hour >= 0; hour--){

                  startMin = 59;

              for (min = startMin; min >= 0; min--){

                  startSec = 59;

                  for(sec = startSec; sec >= 0; sec--){                   

                      try {

                          TimeUnit.SECONDS.sleep(1);

                      } catch (InterruptedException ex) {

                        

                      }

                    jTextField1.setText(hour + ":" + min + ":" + sec); 

                  }

              }

          }

           

    }}

      

    } 

(Might update if needed.)

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked due to inactivity on Sep 21 2017
Added on Aug 22 2017
4 comments
3,254 views