Results 1 to 5 of 5
- 07-20-2007, 10:49 AM #1
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
Using threads as a countdowntimer
After my classes on java I wanted to explore more about the functions of threads.
Eversince after the last days of my classes, they introduce to us the use of threads and create a program that gets the desktop date and time and displays it on a simple frame.
The question is this, can I utilize threads as runnable only when you press the button? And also, I tried looking for a way on displaying a countdown timer. Is there a variable that acts hours,mins,sec accordingly?
I tried to make a for loop in its place just like in assembly only to realized that it has a delay function that countdown in micro sec.
- 07-20-2007, 11:51 AM #2
Yes. Just create the thread in the constructor of your JFrame an call start method when the button pressed!can I utilize threads as runnable only when you press the button?
Yes, keep the number of seconds remaining in a variable and decrease it every seconds! You can call Thread.sleep(X) method to sleep X number of milliseconds.Is there a variable that acts hours,mins,sec accordingly?
You can keep the above variable in your thread and decrease its value and call a method to update the representation. For example representation can be a JLabel and you can call its setText() method in your update method!
- 07-20-2007, 11:59 AM #3
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
- 07-20-2007, 12:19 PM #4
Actually, i did not think when and how you will stop the thread there. It is easy, since you will a reference to your thread object in JFrame. You will just implement a method like stopThread() in your thread and call that method from your JFrame whenever you need.I may be wrong but isn't this a code that tells the thread to stop like when the times up the thread close like destroy();?
So in this scheme, you will use JFrame as the controller and the Thread will only be used to update numberOfSecondsRemaining variable and calling View class (your JFrame) whenever you want to update the display.
So the main structure will be like this (This is just to show you the structure! It will not work if you copy/paste it. You need to handle exceptions, events properly.):
Java Code:class YourFrame extends JFrame { JLabel counterLabel; YourThread thread; public YourFrame() { thread = new YourThread(); } void startButtonClicked() { thread.start(); } void stopButtonClicked() { thread.stopThread(); } void updateView(int remainingSeconds) { counterLabel.setText(remainingSeconds); } }Java Code:class YourThread extends Thread { JFrame frame; int remainingSeconds = 100; boolean running = true; public YourThread(JFrame frame) this.frame = frame; } public void run() { while(running) remainingSeconds--; frame.updateView(remainingSeconds); sleep(...); } public void stopThread() { running = false; } }
- 07-20-2007, 01:06 PM #5
Member
- Join Date
- Jul 2007
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
How to use Java threads
By Java Tip in forum java.langReplies: 0Last Post: 04-09-2008, 06:30 PM -
applets & threads
By willemjav in forum Java AppletsReplies: 2Last Post: 04-04-2008, 06:59 AM -
Using threads
By Java Tip in forum Java TipReplies: 0Last Post: 12-11-2007, 10:25 AM -
Threads
By one198 in forum Threads and SynchronizationReplies: 1Last Post: 11-20-2007, 06:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks