Link to home
Start Free TrialLog in
Avatar of selina3110
selina3110

asked on

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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You seem to have left elements out: where is this time of which you want to show how much is remaining?
Avatar of selina3110
selina3110

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks man!! you got what i needed!
:-)