0

How do I update a panel every decrement of hours, minutes and seconds. The given code works fine with CMD-based display but not with panels.

            if((!_hour.equals(""))&&(!_minute.equals(""))&&(!_second.equals("")))
            {
                frame.dispose();
                //Timer time = new Timer(1000, new TimerListener());
                hour = Integer.parseInt(hours.getText());
                minute = Integer.parseInt(minutes.getText());
                second = Integer.parseInt(seconds.getText());   

                hour = ((hour>=0 && hour<24) ? hour : 0);
                minute = ((minute>=0 && minute<60) ? minute : 0);
                second = ((second>=0 && second<60) ? second : 0);   

                while(second > 0)
                {
                    try
                    {
                        Thread.sleep(1000);
                        second--;
                        timeleft.setText("Time left: " +obj.setTime(hour, minute, second)); 
                        if(second == 0)
                        {
                            while(minute > 0)
                            {
                                try
                                {
                                    Thread.sleep(60000);
                                    minute--;
                                    timeleft.setText("Time left: " +obj.setTime(hour, minute, second)); 
                                        if(minute == 0)
                                        {
                                        while(hour > 0)
                                        {
                                            try
                                            {
                                                Thread.sleep(3600000);
                                                hour--;
                                                timeleft.setText("Time left: " +obj.setTime(hour, minute, second)); 
                                            }
                                            catch(InterruptedException err)
                                            {
                                                err.printStackTrace();
                                            }
                                        }
                                        }                                   
                                }
                                catch(InterruptedException err)
                                {
                                    err.printStackTrace();
                                }
                            }
                        }

                    paneltimer.add(timeleft);                           
                    timer.add(paneltimer);                          
                    timer.setUndecorated(true);
                    timer.setVisible(true);
                    timer.pack();
                    timer.setAlwaysOnTop (true);
                    timer.setLocation(500, 0);  
                    }

                    catch(InterruptedException err)
                    {
                        err.printStackTrace();
                    }
                }
            }

It should display something like this(but seconds should be, for example 3 if I have inputted 3 but in my case it would output 2 initially) and the panel should update every time the seconds, minutes and hours are being decremented however it just stays as displayed below:

enter image description here

Alongside that, it gives the following exception.

enter image description here

Please tell me how to update a panel based on my implementation thanks!

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • This sort of question gets asked [again and again and again](https://www.google.com/webhp?sourceid=chrome-instant&rlz=1C1LEND_enUS445US445&ion=1&espv=2&ie=UTF-8#q=java+swing+thread.sleep+freezes+site:http:%2F%2Fstackoverflow.com%2F), and just like the previous similar questions, use a [Swing Timer](http://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) to drive your animation (which is what this is). – Hovercraft Full Of Eels Aug 22 '15 at 03:04
  • Can you explain how? I don't understand timers fully. I'm sorry. – Friency Fernandez Aug 22 '15 at 03:05
  • 1
    Please read the tutorial and then practice with the code. The main issue -- you shouldn't be calling `Thread.sleep(...)` from within a Swing GUI as you're doing. Either use a background thread (somewhat complex) or a Swing Timer (much easier) to drive your animation loop. – Hovercraft Full Of Eels Aug 22 '15 at 03:05
  • Okay. Actually Why I used sleep is because I am confused about timers but I will study again thanks. – Friency Fernandez Aug 22 '15 at 03:08
  • [Some of my answers that involve use of a Swing Timer](http://stackoverflow.com/search?tab=votes&q=user%3a522444%20swing%20timer) – Hovercraft Full Of Eels Aug 22 '15 at 03:22

0 Answers0