0

I already have the code for start button - already made a reference to the JLabels. But I am in trouble coding for the pause/resume and stop. I also searched for the "schedule" function but I don't know how to implement it. Please help.

  /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package com.pingol.thread;

    import java.util.logging.Level;
    import java.util.logging.Logger;
    import javax.swing.JLabel;


    public class Timer implements Runnable {

        JLabel miliseconds;
        JLabel seconds;
        JLabel minute;

        public Timer(JLabel miliseconds, JLabel seconds, JLabel minute){
            this.miliseconds = miliseconds;
            this.seconds = seconds;
            this.minute = minute;
        }
        @Override
        public void run() {
            for (int i = 0; i < 60; i++){ 
                this.minute.setText(Integer.toString(i));
                for (int j = 0; j < 60; j++) {
                      this.seconds.setText(Integer.toString(j));
                    for (int k = 0; k < 60; k++) {
                        this.miliseconds.setText(Integer.toString(k));
                          try {
                              Thread.sleep(10);
                          } catch (InterruptedException ex) {
                              Logger.getLogger(Timer.class.getName()).log(Level.SEVERE, null, ex);
                          }
                    }
                }
            }
        } 
    }
/*
FRAME (s**strong text**tart):

private void startActionPerformed(java.awt.event.ActionEvent evt) {                                      
        Timer timer = new Timer(miliseconds,seconds,minutes);
        Thread th = new Thread(timer);
        th.start();
    }  
*/
Ji L
  • 3
  • 2
  • Nothing to do with your question, but all calls to Swing should be done with [`EventQueue.invokeLater(Runnable r);`](http://stackoverflow.com/questions/22534356/java-awt-eventqueue-invokelater-explained) when you are calling from another thread. Example: `EventQueue.invokeLater(() -> minute.setText(i));` – Jhonny007 Sep 12 '16 at 14:11
  • You should be aware that `Thread.sleep(10)` is guaranteed to sleep for _at least_ ten milliseconds. It could sleep longer depending on what JVM version you are running and, what operating system version and, how your OS is configured. Also, your code does not account for how much time might be spent in any of those `.setText(...)` calls. – Solomon Slow Sep 12 '16 at 21:47

1 Answers1

0

Make your thread variable a class variable. Then in your startActioPerformed call th.start(). In your pauseActionPerformed call th.wait(); and in your resumeActionPerform call th.resume();

Petros
  • 550
  • 3
  • 13