This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Java Timer & Clock" Watch "Java Timer & Clock" New topic
Author

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( );
timeLabel.setBorder(border);
dateLongLabel.setPreferredSize(new Dimension(100,30));
timeLabel.setText("TIME ");
timeLabel.setBackground(Transparent);

Box sbar = Box.createHorizontalBox();
.......
.......
sbar.add(timeLabel);
add(sbar);

class UpdateTime extends java.util.TimerTask {
public void run () {
final DateFormat df = new SimpleDateFormat("HH:mm.ss" + " " + 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

 
wood burning stoves
 
subject: Java Timer & Clock
 
Similar Threads
'Stale' image reference is retained by JFrame desktop app--latest image won't display
Menu Items are not appearing.
How to display time
Clock
How to add a Status Bar in a frame(at the Bottom ofcourse)