• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

CountDown timer in java swings

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Please help me in developing countdown timer in java swings displaying remaining time left in hours and seconds as 00h20min.

Thanks in advance.

Regards,
Seema
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you done so far?
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have the following code with me. I need to include time left in 00h00mins instead of percentage of task completed.

public class ProgressMonitorExample extends JFrame implements ActionListener {

static ProgressMonitor pbar;

static int counter = 0;

public ProgressMonitorExample() {

super("Progress Monitor Demo");

setSize(250, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pbar = new ProgressMonitor(null, " ",
"Initializing . . .", 00, 100);

//Fire a timer every once in a while to update the progress.
Timer timer = new Timer(500, this);

timer.start();
setVisible(true);
}

public static void main(String args[]) {
UIManager.put("ProgressMonitor.progressText", "Demo");
UIManager.put("OptionPane.cancelButtonText", "Go Away");

new ProgressMonitorExample();
}

public void actionPerformed(ActionEvent e) {
// Invoked by the timer every half second. Simply place
// the progress monitor update on the event queue.
SwingUtilities.invokeLater(new Update());
}

class Update implements Runnable {
public void run() {
if (pbar.isCanceled()) {
pbar.close();
System.exit(1);
}
pbar.setProgress(counter);
pbar.setNote("Operation is " + counter + "% complete");
counter += 1;
}
}
}


Please help to implement.

Regards,
Seema
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You haven't got very far, have you?

Work out where you can get the time from, particularly since you may be able to get it from the System of your computer. Work out how to calculate time left in minutes and seconds. I am a bit surprised you are using 500 for your Timer rather than 1000!
Then let's see what you have got.
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have the following code after including seconds count down timer. What i have observed is:
1. Timer does not stop and goes to infinite(in negatives)
2.Progress bar blocks are full
3. In Progress monitor, arguments cannot be changed to set maximum value as 0 and hence progress bar blocks are at maximum from first.

package ProgressBar;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;

import javax.swing.ProgressMonitor;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ProgressMonitorExample_timer extends JFrame implements
ActionListener {

ProgressMonitor pbar;

int count = 100;

static int counter = 0;

int hours = 0;

int min = 0;

int timeRemaining = 24;

public ProgressMonitorExample_timer() {

super("Progress Monitor Demo");

setSize(250, 100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pbar = new ProgressMonitor(null, " ", "Initializing . . .", 00, 100);

Timer timer = new Timer(1000, this);

timer.start();
setVisible(true);
}

public static void main(String args[]) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
UIManager.put("ProgressMonitor.progressText", "Demo");
UIManager.put("OptionPane.cancelButtonText", "Go Away");

new ProgressMonitorExample_timer();
}

public void actionPerformed(ActionEvent e) {
// Invoked by the timer every half second. Simply place
// the progress monitor update on the event queue.
SwingUtilities.invokeLater(new Update());
}

class Update implements Runnable {
public void run() {
if (pbar.isCanceled()) {
pbar.close();
System.exit(1);
}

pbar.setProgress(count);

pbar.setNote("Operation left " + count + "");

--count;
}

}
}


Please correct me and guide me.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please use CODE tags round your code; it makes it easier to read.
You don't seem to have any code which stops the Timer hen it reaches 0.
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please check the code. Progress bar is not displayed properly. I am not able to set maximum to minimum range. Progress monitor constructor is accepting only min to max. Interchanging these values will not make progress bar to work.

[B][/B]

Thanks in advance.

Regards,
Seema
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go through the API documentation for JProgressBar, where there is a link to the Java Tutorial about how to use progress bars, and also a link to BoundedRangeModel where you find that the maximum must be greater than the minumum. PogressMonitor appears to be less strict, but I don't think it is intended to count backwards. You will have to get its source code and see what you can work out; find your Java installation directory, find the file called src.zip or similar, unzip it and explore it to find ProgressBar.

How are you stopping the Timer when you reach 0?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
try with the following code


if(--count==0)
{
system.exit(1);
}
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try not to kick threads that are months old. For all we know Seema is not even active anymore.
reply
    Bookmark Topic Watch Topic
  • New Topic