my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes CountDown timer in java swings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "CountDown timer in java swings" Watch "CountDown timer in java swings" New topic
Author

CountDown timer in java swings

Seema Sharma
Ranch Hand

Joined: May 24, 2007
Posts: 78
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
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
What have you done so far?
Seema Sharma
Ranch Hand

Joined: May 24, 2007
Posts: 78
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
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
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

Joined: May 24, 2007
Posts: 78
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
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
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

Joined: May 24, 2007
Posts: 78
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
Sheriff

Joined: Oct 13, 2005
Posts: 39079
    
  23
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?
peer mohamed fazil
Greenhorn

Joined: Jan 26, 2009
Posts: 1
try with the following code


if(--count==0)
{
system.exit(1);
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Try not to kick threads that are months old. For all we know Seema is not even active anymore.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
GeeCON Prague 2014
 
subject: CountDown timer in java swings