Want to implement a clock which updates every second

J

Joseph Jude

Hi,

I have a problem. I have written a date and clock function which
displays the clock on the main screen of my java apllication. It
retreives the server time during startup. What i want to do is, the
clock should keep on refreshing itself every second so that the user
will be able to track the exact time. Is there any way out? responses
will be greatly appreciated.
 
A

Andrew Thompson

I have a problem. ..

Your first problem is that judging the vague question
<http://www.physci.org/codes/javafaq.jsp#specific>
...you ask, you should be posting on c.l.j.help.
..I have written a date and clock function which
displays the clock on the main screen of my java apllication.

..It retreives the server time during startup.

What server? Is this an application? applet? web app?
..What i want to do is, the
clock should keep on refreshing itself every second so that the user
will be able to track the exact time. Is there any way out?

Yes, there undoubtedly is.

More specific information is required for
a more specific answer though.
 
R

Roedy Green

I have a problem. I have written a date and clock function which
displays the clock on the main screen of my java apllication. It
retreives the server time during startup. What i want to do is, the
clock should keep on refreshing itself every second so that the user
will be able to track the exact time. Is there any way out? responses
will be greatly appreciated.

Just use a Timer that goes off every 10th of a second or so. poke the
time into a JLabel or JtextField.

I have always wanted to experiment to see if these components are
smart enough to do nothing if you setText the same value they have
already. I wondered if there was any point in having my own code
avoid poking them when the value did not change.

Ditto for Checkbox.setState etc.
 
L

Liz

Joseph Jude said:
Hi,

I have a problem. I have written a date and clock function which
displays the clock on the main screen of my java apllication. It
retreives the server time during startup. What i want to do is, the
clock should keep on refreshing itself every second so that the user
will be able to track the exact time. Is there any way out? responses
will be greatly appreciated.
------------------------------------------------------
This is my version of a class that I found in a book.
instead of
JLabel xxx = new JLabel("xxx",SwingConstants.CENTER);
use this
ClockLabel xxx = new ClockLabel("xxx", SwingConstants.CENTER);
The timer fires every 1 second, but since that is the resolution
of the display format it is ok.
------------------------------------------------------
// class to create a JLable and put the time of day into it (refresh every
1 second)
public class ClockLabel extends JLabel implements ActionListener
{
SimpleDateFormat formatter = new SimpleDateFormat("MM/dd/yy HH:mm:ss");
public ClockLabel(String text, int horizontalAlignment)
{
super(text, horizontalAlignment);
Timer t = new Timer(1000, this);
t.start();
}
public void actionPerformed(ActionEvent e)
{
setText((formatter.format(new Date())).toString());
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top