This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Java Timer & Clock

 
Timothy Leong
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic