1

How can I get the time left in a util.Timer? What I want to do is to add a progressbar that displays time left until the timer starts over.

This is what I've got this far:

int seconds = 8;

java.util.Timer timer = new Timer();
timer.schedule( new TimerTask(){
    public void run(){
        // Do something
        // Add a progressbar that displays time left until the timer "starts over".
    },0,(long) (seconds*1000));
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
thorbear
  • 15
  • 1
  • 1
  • 4

2 Answers2

4

You would need a second timer to refresh the gui in a specific interval.
Another way to achieve this, would be to activate a single timer every second and update the counting in the ui. If the time is up, call your specific action.

A simple expample with console output only:

TimerTask task = new TimerTask()
{
    int seconds = 8;
    int i = 0;
    @Override
    public void run()
    {
       i++;

       if(i % seconds == 0)
           System.out.println("Timer action!");
       else
           System.out.println("Time left:" + (seconds - (i %seconds)) );
    }
};

Timer timer = new Timer();
timer.schedule(task, 0, 1000);

It's output would be:

Time left:7
Time left:6
Time left:5
Time left:4
Time left:3
Time left:2
Time left:1
Timer action!
Time left:7
Time left:6
Time left:5
Time left:4
Time left:3
Time left:2
Time left:1
Timer action!
Time left:7
Time left:6
...

Then simply change the System.out's with your code to update the progress bar. Remember: java.util.Timer starts its own Thread. Swing is not thread safe, so you need to put every gui changing code into SwingUtilities.invokeLater()!
If you're not doing any long running tasks, every time your timer reachs the 8 seconds mark, you may want to use javax.swing.Timer directly. It uses the EDT and not its own Thread, so you don't need to synchronize your calls to Swing components with SwingUtilities.invokeLater().

Also see:
javax.swing.Timer vs java.util.Timer inside of a Swing application

Community
  • 1
  • 1
Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • Thak you! This was very helpful! I am also wondering: Is ther any way to make this work in different speeds? For exsample after ten times, the value of seconds is changed? – thorbear Feb 16 '15 at 14:36
  • You could always add additional checks with sth. like `if(i == seconds * 10) seconds = 20; // on the 10th trigger, set seconds to 20`, but you may have to reset the counter or introduce another counting variable to make it work correctly. but if you just double the seconds, you will be fine. there is some extra logic needed. ` – Zhedar Feb 16 '15 at 14:46
  • I am using this together with a Jprogressbar and I have a textbox that takes whatewer stands inside it as an answer to a question when the time runs out. I am having two problems with this program. The first one is that it crashes if I am typing in the textfield when the timer starts over. Everything freezes. This is a problem when I start to decrease the amount of time left. The second problem I'm having, is that sometimes, the time left (Jprogressbar) jumps ahead a little bit. I can't find out what causes this. Any ideas would be appreciated! :) – thorbear Mar 05 '15 at 10:55
0

All you need to do is declare a long variable timeleft in your MainActivity.

long timeleft; 

Then, when you create a new Timer, set the "onTick" override to update the timeleft variable each "onTick" (which in the following example is 1000 milliseconds )

        timer = new CountDownTimer(time, 1000) {
           @Override
            public void onTick(long millisecondsUntilFinished) {
                timeleft = millisecondsUntilFinished;
            }
          }

Your app can access then the variable timeleft every time you need to check how much time is left.

Lucas Cave
  • 73
  • 8
  • 1
    For anyone trying to use this, CountDownTimer is part of the Android SDK, not Java, which may or may not be appropriate for your needs. https://developer.android.com/reference/android/os/CountDownTimer – Allen Nov 08 '21 at 02:38