File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Java Timer & Clock Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Timer & Clock" Watch "Java Timer & Clock" New topic

Java Timer & Clock

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
Hi guys, I have a status bar created using JLabels in StatusBar class.

This status bar can display the current system time.
This status bar is added onto JFrame. My JFrame has other components like JToolBar, JMenubar another JPanel etc.

I tried using Timer in my StatusBar class. However, I experience lag on my screen. It actually takes 20 sec for the clock to actually work.
How can I make my clock to work straight away? Thanks

public class StatusBar extends JPanel
DateTime currDateTime = new DateTime();
java.util.Timer fTimer = null;

JLabel messageLabel;
JLabel userLabel;
JLabel dateLongLabel;
JLabel timeLabel;

public int i =0;

public StatusBar()
fTimer = new java.util.Timer ();
Color Transparent=new Color(0,0,0,0);
Border border = LineBorder.createGrayLineBorder();

timeLabel = new JLabel( );
dateLongLabel.setPreferredSize(new Dimension(100,30));
timeLabel.setText("TIME ");

Box sbar = Box.createHorizontalBox();

class UpdateTime extends java.util.TimerTask {
public void run () {
final DateFormat df = new SimpleDateFormat("" + " " + i);
timeLabel.setText(df.format(new Date()));
fTimer.schedule (new UpdateTime (), 0, 1000);

Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
sorry just to add.

The clock not only lag and it also causes my other components in JFrame to be distorted as well.
Timothy Leong
Ranch Hand

Joined: May 25, 2005
Posts: 55
after countless trial and error
I realise it's the bold coding that causes the lag and distortion of my GUI
However I still unable to solve the problem

Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
not sure what you're trying to do - a lot of your code seems unrelated to
the stated problem.

anyway, seesm like you're trying to display the current time in perhaps
the bottom left corner of the frame.

if so, here's a simple demo

I agree. Here's the link:
subject: Java Timer & Clock
It's not a secret anymore!