Help on count down timer

Hi experts, i need to create a simple timer that will countdown showing the remaing time display in hh:mm:ss.

i only manage to display to get it display in seconds and I can't get it to countdown. Hope someone would able to help to modify my codes to get my requirement working. Thanks in advance!


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
 
public class TimerDisplay extends JLabel implements ActionListener{
 int count = 1000;
 
public TimerDisplay()
       {        
        super("Remaining Time: 0 secs", JLabel.CENTER);
        Timer t = new Timer(1000, this);  t.start();
		
		
    }
 public void actionPerformed(ActionEvent e){
        this.setText("Remaining Time: " + count++ + " secs");
    }    
 public static void main (String arg[]){
   JFrame f = new JFrame();
   f.setSize(300,100);
   f.addWindowListener (new WindowAdapter()
     { public void windowClosing(WindowEvent e){
       System.exit(0);}});
   TimerDisplay c = new TimerDisplay();
   f.getContentPane()
    .setLayout(new BorderLayout());
   f.getContentPane()
    .add(c, BorderLayout.CENTER);
   f.show();
 } }

Open in new window

selina3110Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
You seem to have left elements out: where is this time of which you want to show how much is remaining?
0
selina3110Author Commented:
I wanted to let the program countdown, for example 01 HR 30 MIN 30 SEC till 00 HR 00 MIN 00 SEC but i got no idea how to go about doing it.
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
 
public class TimerDisplay extends JLabel implements ActionListener{
 int count = 1000;
 
public TimerDisplay()
       {        
        // suppose to show as in 01 HR 30 MIN 30 SEC.
        super("Remaining Time: 0 secs", JLabel.CENTER); 
        Timer t = new Timer(1000, this);  t.start();        
                
    }
 public void actionPerformed(ActionEvent e){
        // suppose to countdown till OO HR 00 MIN 00 SEC
        this.setText("Remaining Time: " + count++ + " secs");
    }    
 public static void main (String arg[]){
   JFrame f = new JFrame();
   f.setSize(300,100);
   f.addWindowListener (new WindowAdapter()
     { public void windowClosing(WindowEvent e){
       System.exit(0);}});
   TimerDisplay c = new TimerDisplay();
   f.getContentPane()
    .setLayout(new BorderLayout());
   f.getContentPane()
    .add(c, BorderLayout.CENTER);
   f.show();
 } }

Open in new window

0
CEHJCommented:
Here you go
import java.awt.*;
import java.awt.event.*;
 
import java.text.*;
 
import java.util.*;
 
import javax.swing.*;
 
 
public class TimerDisplay extends JLabel implements ActionListener {
    private long count;
    private int hours;
    private int minutes;
    private int seconds;
    private DateFormat df;
 
    public TimerDisplay(int hours, int minutes, int seconds) {
	// suppose to show as in 01 HR 30 MIN 30 SEC.
	super("Remaining Time: 0 secs", JLabel.CENTER);
 
	Calendar cal = Calendar.getInstance();
	cal.set(Calendar.HOUR, hours);
	cal.set(Calendar.MINUTE, minutes);
	cal.set(Calendar.SECOND, seconds);
	count = cal.getTime().getTime();
	df = new SimpleDateFormat("hh:mm:ss");
 
	javax.swing.Timer t = new javax.swing.Timer(1000, this);
	t.start();
    }
 
    public void actionPerformed(ActionEvent e) {
	// suppose to countdown till OO HR 00 MIN 00 SEC
	setText(df.format(count));
	count -= 1000;
    }
 
    public static void main(String[] arg) {
	JFrame f = new JFrame();
	f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	f.setSize(300, 100);
 
	TimerDisplay c = new TimerDisplay(1, 30, 30);
	f.getContentPane().add(c);
	f.setVisible(true);
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
selina3110Author Commented:
Thanks man!! you got what i needed!
0
CEHJCommented:
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.