0

So far I have implemented a timer in a thread which counts 6 to 0. I would like to show the progress of the java application i.e., I would like to let the user know how much time it would take for the application to run. I am looking to continuously update the time like we have a progress bar when we download a software which shows 1 minute, 59 seconds so on. I would like update the time remaining based on the time required for the program to execute rather than a predefined time frame. Thanks in advance.

public void toDisplayTime(){
    TimerTask task = new TimerTask() {
        int seconds = 6;
        int i = 0;
        @Override
        public void run() {
            i++;
            if (i % seconds == 0) {
                updateTime("Reset");                    
            } else {
                updateTime("Time left:" +(seconds - (i % seconds)));                    
            }
        }
    };

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

updateTime() is below.

public void updateTime(String updateString) {
   SwingUtilities.invokeLater(new Runnable() {
      public void run() {
           jLabel6.setText(updateString);
      }
   });
}
Nikitha
  • 373
  • 2
  • 18
  • not sure in your case, Take a look at [SwingWorker](https://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html) – ThoFin Feb 10 '16 at 17:12
  • That question has 10 as the counter. I would like to dynamically update the counter depending on the time required for the program to run @Berger – Nikitha Feb 10 '16 at 17:13
  • `totalTime * pctDone/100 = timeSoFar` ➡`totalTime = timeSoFar * 100 / pctDone`, so if you know progress (`pctDone`) and elapsed time (`timeSoFar`), you can calculate `totalTime` and hence time left: `timeLeft = totalTime - timeSoFar`. Now you just need to format it for display. – Andreas Feb 10 '16 at 17:17
  • @Andreas I didn't understand what you were trying to tell me. Could you please elaborate. I know its tough for you to post your code in the comments section – Nikitha Feb 10 '16 at 17:20
  • @NikithaReddy You said you want to show "how much time it would take for the application to run". Unless you somehow know exactly how long that will be, you have to calculate (estimate/extrapolate) it. My previous comment was about a simple formula for doing that. E.g. in a file download, you don't know how long the download will take, but assuming that download speed is consistent, you can calculate total download time by extrapolating from how long it's taken to download as much as you have so far, assuming you know the total size of the download. – Andreas Feb 10 '16 at 17:28
  • Why go through the rigmarole of a java.util.Timer and Swing threading? Simply use a Swing Timer and you'd simplify this code by half. – Hovercraft Full Of Eels Feb 10 '16 at 17:31
  • I understand your explanation now @Andreas. But my application is a stand alone one. It is just dependent on the events performed under a single button click. Should I just assume based on previous executions of the application? – Nikitha Feb 10 '16 at 17:34
  • @NikithaReddy I don't know you app. You said to show time left, but if remaining execution time is mainly based on the user pressing a button X times, you really cannot show "time" left, since users may take as long as they want before pressing the next button. Trying to show "time" is just gonna be misleading. – Andreas Feb 10 '16 at 17:40
  • Let me be clear. User clicks on Generate Output button. Once user clicks on that button, the process starts to execute. I want to display the time that the process would take to run. – Nikitha Feb 10 '16 at 17:42
  • 1
    And how do you know how long that will be? Each execution of the program may take a totally different amount of wall-clock time, The time will also depend on what other programs and daemons are running at the same time – FredK Feb 10 '16 at 19:53
  • I see that I can't calculate time. I was curious if I can do that. Thanks everyone – Nikitha Feb 10 '16 at 19:57
  • See all this related [example](http://stackoverflow.com/a/12451673/230513). – trashgod Feb 10 '16 at 21:40

0 Answers0