Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-20-2007, 11:49 AM
Member
 
Join Date: Jul 2007
Posts: 3
lacoste is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-20-2007, 12:51 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
can I utilize threads as runnable only when you press the button?
Yes. Just create the thread in the constructor of your JFrame an call start method when the button pressed!

Quote:
Is there a variable that acts hours,mins,sec accordingly?
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.

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!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-20-2007, 12:59 PM
Member
 
Join Date: Jul 2007
Posts: 3
lacoste is on a distinguished road
Ahh thank you sir. Im just having a hard time in net beans, so I thought this was not possible.

About the last one:
Quote:
Originally Posted by JavaBean View Post
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.

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!
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();?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-20-2007, 01:19 PM
JavaBean's Avatar
Moderator
 
Join Date: May 2007
Posts: 1,272
JavaBean is on a distinguished road
Quote:
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();?
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.

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.):

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); } }
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; } }
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-20-2007, 02:06 PM
Member
 
Join Date: Jul 2007
Posts: 3
lacoste is on a distinguished road
ahh i c so thats how thanks, ill try it right away thank you.....
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use Java threads Java Tip java.lang 0 04-09-2008 07:30 PM
applets & threads willemjav Java Applets 2 04-04-2008 07:59 AM
Java Threads JavaForums Java Blogs 0 12-17-2007 01:21 PM
Using threads Java Tip Java Tips 0 12-11-2007 11:25 AM
Threads one198 Threads and Synchronization 1 11-20-2007 07:15 PM


All times are GMT +3. The time now is 08:26 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org