My issue is I can't get the current time to start counting by seconds for example 22:17:51 - 22:17:52 - 22:17:53. Any help will be greatly appreciated and here is my code so far. I can get the time to display just not start.
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.Timer; import java.util.Date; import java.text.DateFormat; import java.text.SimpleDateFormat; public class TimeCounter extends JFrame { //instance variables JLabel counterLabel; int count = 1; //inner class to listen to Timer DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss "); //get current date time with Date() Date date = new Date(); class TimerListener implements ActionListener { public void actionPerformed(ActionEvent event) { counterLabel.setText(dateFormat.format(date)); } } //constructor for SecondCounter and JFrame public TimeCounter() { setSize(200, 100); counterLabel = new JLabel("Date: "+date); add(counterLabel); ActionListener listener = new TimerListener(); // Milliseconds between timer ticks final int DELAY = 1000; Timer t = new Timer(DELAY, listener); t.start(); //start the time } }
then the driver
Thanks for taking the time to look at my code and help.