Results 1 to 5 of 5
  1. #1
    lacoste is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default 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.

  2. #2
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

    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!

    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!

  3. #3
    lacoste is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default

    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();?

  4. #4
    JavaBean's Avatar
    JavaBean is offline Moderator
    Join Date
    May 2007
    Posts
    1,270
    Rep Power
    9

    Default

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

    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;
    }
    
    }

  5. #5
    lacoste is offline Member
    Join Date
    Jul 2007
    Posts
    3
    Rep Power
    0

    Default

    ahh i c so thats how thanks, ill try it right away thank you.....

Similar Threads

  1. How to use Java threads
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-09-2008, 06:30 PM
  2. applets & threads
    By willemjav in forum Java Applets
    Replies: 2
    Last Post: 04-04-2008, 06:59 AM
  3. Using threads
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-11-2007, 10:25 AM
  4. Threads
    By one198 in forum Threads and Synchronization
    Replies: 1
    Last Post: 11-20-2007, 06:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •