Sajad
Sajad

Reputation: 2363

My clock not show second increasing

Why is my clock not showing the seconds increasing?

public class timeTest extends javax.swing.JFrame {

    public timeTest() {
        initComponents();
        showTime();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new timeTest().setVisible(true);
            }
        });
    }

    private javax.swing.JLabel timeLable;

    private void showTime() {
        Timer timer = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar time = Calendar.getInstance();
                timeLable.setText(time.getTime().toString());
            }
        });
        timer.setRepeats(false);
        timer.start();
    }
}

I haven't written the code, it where generated by the IDE.

Upvotes: 1

Views: 176

Answers (2)

Festus Tamakloe
Festus Tamakloe

Reputation: 11310

replace

timer.setRepeats(false);

with

timer.setRepeats(true);

I just add a date formatter for pretty look

public class timeTest extends javax.swing.JFrame {

  SimpleDateFormat date_format = new SimpleDateFormat("MMM dd, yyyy HH:mm:ss");

    public timeTest() {
        initComponents();
        showTime();
    }

    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new timeTest().setVisible(true);
            }
        });
    }

    private javax.swing.JLabel timeLable;

    private void showTime() {
        Timer timer = new Timer(100, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Calendar time = Calendar.getInstance();
                timeLable.setText(date_format.format(time.getTime()));
            }
        });
        timer.setRepeats(true);
        timer.start();
    }
}

Upvotes: 2

Tom
Tom

Reputation: 16198

Your timer doesn't repeat.

timer.setRepeats(true);

Will set it to repeat.

Actually you don't even need this, it is set to true by default.

Upvotes: 7

Related Questions