• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Countdown timer in java

 
Peter Zagrzebski
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello I have two classes in java and am attempting to countdown to 0. I cannot change the code in minute second display and am having difficulty getting it to move from 10 to 0 on the display in minute second display because it calls invokeLater to Update the display and that does no get done until the end. If you could look at my code and give me any advice I would appreciate it.



 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling the countdown() method directly from the actionPerformed() method, which executes in your event dispatch thread - the same thread to used to draw on the screen. Since you are performing the Thread sleeping inside countdown the Event Thread won't be able to do the work it is intended to do - update the GUI.

So you should move the countdown() code to a different thread, so that the GUI interface isn't blocked while counting.

You are also doing some redundant context switching (with SwingUtilities.invokeLater() to call a method that itself uses SwingUtilities.invokeLater()), and creating objects you don't use (for example the t = new Thread(count)). So your code has a lot of clean-up which can be done.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic