• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5070
  • Last Modified:

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

0
selina3110
Asked:
selina3110
  • 3
  • 2
1 Solution
 
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
 
selina3110Author Commented:
Thanks man!! you got what i needed!
0
 
CEHJCommented:
:-)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now