Gnosis
Gnosis

Reputation: 61

Java Getting Clock To Update

I'm working on a project for school, and I'm sorta stuck. I have written a program the displays the current time, but I can't figure out how to get it to update as the time changes. If some could help me, or point me in some direction to get started, your help will be appreciated. I will post what I have written so far below.

import javax.swing.*;
import java.awt.*;
import java.util.*;


public class Project2 extends JFrame{
public static void main(String[] args){
    Project2 myFrame = new Project2();
    myFrame.pack();
    myFrame.setTitle("Digital Clock");
    myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    myFrame.setLocationRelativeTo(null);
    myFrame.setVisible(true);


}//main()

public Project2(){
System.out.println(currentTime());
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout());
JLabel time = new JLabel(currentTime());
time.setFont(new Font("TimesRoman", Font.BOLD, 20));
time.setForeground(Color.blue);
p1.add(time);
this.setLayout(new BorderLayout());
this.add(p1, BorderLayout.CENTER);

}

public String currentTime(){
    Calendar calendar = Calendar.getInstance();
   int hours = calendar.get(Calendar.HOUR_OF_DAY);
   int minutes = calendar.get(Calendar.MINUTE);
   int seconds = calendar.get(Calendar.SECOND);
   int aP = calendar.get(Calendar.AM_PM);
   String currentTime = hours+":"+checkTime(minutes)+":"+checkTime(seconds)+" "+amP(aP);
   return currentTime;
 }

public String checkTime(int t){
    String time1;
    if (t < 10){
        time1 = ("0"+t);
        }
    else{
        time1 = (""+t);
        }
    return time1;
}

public String amP(int ap){
    String amPm;
    if( ap == 0)
        amPm = "AM";
    else
        amPm = "PM";
    return amPm;
}



}//Project2

Upvotes: 0

Views: 5035

Answers (3)

Manish
Manish

Reputation: 3968

Hint: Have a look at Timer and TimerTask classes. Then use these classes to update the JLabel's text.

Upvotes: 1

Pradeep
Pradeep

Reputation: 1398

Declare JPanel and JLabel as global variables and add the following lines at the end of the constructor

ActionListener taskPerformer = new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    System.out.println(currentTime());
                    time.setText(currentTime());
                }
            };
            Timer t = new Timer(1000, taskPerformer);
            t.start();

Here is the complete code:

import javax.swing.*;
import javax.swing.Timer;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

public class DigitalClock extends JFrame {

    private JPanel p1;
    private JLabel time;

    public static void main(String[] args) {
        DigitalClock myFrame = new DigitalClock();
        myFrame.pack();
        myFrame.setTitle("Digital Clock");
        myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        myFrame.setLocationRelativeTo(null);
        myFrame.setVisible(true);

    }// main()

    public DigitalClock() {
        System.out.println(currentTime());
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());
        time = new JLabel(currentTime());
        time.setFont(new Font("TimesRoman", Font.BOLD, 20));
        time.setForeground(Color.blue);
        p1.add(time);
        this.setLayout(new BorderLayout());
        this.add(p1, BorderLayout.CENTER);
        ActionListener taskPerformer = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                System.out.println(currentTime());
                time.setText(currentTime());
            }
        };
        Timer t = new Timer(1000, taskPerformer);
        t.start();
    }

    public String currentTime() {
        Calendar calendar = Calendar.getInstance();
        int hours = calendar.get(Calendar.HOUR_OF_DAY);
        int minutes = calendar.get(Calendar.MINUTE);
        int seconds = calendar.get(Calendar.SECOND);
        int aP = calendar.get(Calendar.AM_PM);
        String currentTime = hours + ":" + checkTime(minutes) + ":"
                + checkTime(seconds) + " " + amP(aP);
        return currentTime;
    }

    public String checkTime(int t) {
        String time1;
        if (t < 10) {
            time1 = ("0" + t);
        } else {
            time1 = ("" + t);
        }
        return time1;
    }

    public String amP(int ap) {
        String amPm;
        if (ap == 0)
            amPm = "AM";
        else
            amPm = "PM";
        return amPm;
    }

}// Project2

Upvotes: 1

melli-182
melli-182

Reputation: 1234

I am agree with Pradeep, you must use a timer, for example you can define a timer, and make that a method is call every 1 second, in this method you can update the displayed time...

Upvotes: 0

Related Questions