time displayed in JLabel

I would like to have a JLabel display the current time in an application.  I would like the time to update every second ie, i want it to basically function as a clock.  Currently, I have a JPanel with the label in it and I have the time displayed using the Date() class.  The only catch is that the time is not updated.  How do I go about gettig this done?  Thanks a lot.
dgalAsked:
Who is Participating?
 
laaxConnect With a Mentor Commented:


Write a thread something like this:
    class TimeKeeper extends Thread {
        private JLabel target;
        public TimeKeeper(JLabel comp) {
            target = comp;
        }

        public void run() {
            while ( true ) {
                target.setText("<Time string here...>");
                try {
                    // Update time for every second
                    sleep(1000);
                }
                catch ( InterruptedException e ) {
                }
            }
        }
    }

and attach your label with this thread object like this:

    TimeKeeper clock = new TimeKeeper(<Your label object>);
    clock.start();

Laax.
0
 
mzimmer74Commented:
What you'll have to do is have a separate thread that sleeps for a second, gets the current time (by creating a new Date object), and set the text to the JLabel.
0
 
objectsCommented:
Laax,

Thats exactly what mzimmer74 said, proposed solutions are not supposed to duplicate a previous comment.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
objectsCommented:
import javax.swing.*;
import java.util.*;

public class TimeLabel extends JLabel
   implements ActionListener
{
  private DateFormat Display = new SimpleDateFormat("hh:mm:ss");
  private Timer Tick = new Timer(1000, this);

  public TimeLabel()
  {
  }

  public void actionPerformed(ActionEvent event)
  {
    setText(Display.format(new Date());
  }
}
0
 
laaxCommented:
Hi objects,
>proposed solutions are not supposed to duplicate a previous comment.
   I too know that...but this time I failed check for
latest comments/answers before sending the code, since I
had a phone call...lost the flow :-)
0
 
objectsCommented:
ok, I'll let you off this time ;-)
0
 
dgalAuthor Commented:
Laxx-
  I am very new to Java and programming in general so I don't really understand what you mean by thread etc.  I'll check out your solution but do you think you could explain what the code you proposed is doing?  ie, what's a thread etc.  Thanks a lot.
0
 
objectsCommented:
My solutions hides you from the confusion of creating threads etc :-)
0
 
objectsCommented:
And contains a bug :-(
Should be:

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

public class TimeLabel extends JLabel
  implements ActionListener
{
 private DateFormat Display = new SimpleDateFormat("hh:mm:ss");
 private Timer Tick = new Timer(1000, this);

 public TimeLabel()
 {
   Tick.start();
 }

 public void actionPerformed(ActionEvent event)
 {
   setText(Display.format(new Date());
 }
}


All you need to do is add the custom label to your gui:

panel.add(new TimeLabel());
0
 
dgalAuthor Commented:
objects-
  while i like the idea of not using a thread, your code results in the following error "ActionListener of TimeLabel not found..."  Any ideas?  Thanks.
0
 
objectsCommented:
Add the following line to the top:

import java.awt.event.*;
0
 
dgalAuthor Commented:
objects-
  while i like the idea of not using a thread, your code results in the following error "ActionListener of TimeLabel not found..."  Any ideas?  Thanks.
0
 
objectsCommented:
Woops just noticed another missed import:

import javax.swing.*;
import java.util.Date;
import java.awt.event.*;
import java.text.*;

public class TimeLabel extends JLabel
 implements ActionListener
{
private DateFormat Display = new SimpleDateFormat("hh:mm:ss");
private Timer Tick = new Timer(1000, this);

public TimeLabel()
{
  Tick.start();
}

public void actionPerformed(ActionEvent event)
{
  setText(Display.format(new Date()));
}
}
0
 
laaxCommented:
Hi dgal,

   Thead is like your assistant.  You can assign him some task and continue your work(main control flow).

   Assistant expects you to assign the task(Code) inside
the function 'run'  [public void run()]

   Once you say 'Go' to your assistant [clock.start()] he would execute the instructions ( inside run() function)

   In my code, TimeKeeper is one such assistant decidcated
to update date in a given Label Component.  We have to
pass the Label component while creating the assistant
(thread), for him to update the label. [   TimeKeeper clock = new TimeKeeper(<Your label object>);]

   The run() function continously updates the label with latest time, at the interval of one second[ sleep(1000) -
1000 milli sec].

   I hope it helps for time being.  Read more about threads
when you find time.  :-}  Please let me know if you any other questions.

Laax.
0
 
dgalAuthor Commented:
Thank you Laxx.  Your solution seems to work.  Sorry objects but thank you VERY much for your help as well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.