Solved

Simple Clock---Threads

Posted on 2004-08-28
5
225 Views
Last Modified: 2010-03-31
The Thread runs just once.
It Simply Displays the Date once.
It Does not refresh...Why?

Here's the Code



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

public class Clock2 extends JPanel implements Runnable
{

      JLabel label;
      Thread thread;
      public Clock2()
      {
      label=new JLabel();
      addLabel();
      }

      public void addLabel()
      {
      add(label);
      thread=new Thread(this,"Clock");
      thread.start();
      }

      public void run()
      {
      Thread mythread=Thread.currentThread();

      if (mythread==thread)
            {
                  System.out.println("In");
                  Calendar cal=Calendar.getInstance();
                  label.setText(cal.getTime().toString());
                  try{      Thread.sleep(200);  System.out.println("Up");}
                  catch(Exception e){}
            }
      }

      public static void main(String args[])
      {
            JFrame frame=new JFrame();
            frame.getContentPane().add(new Clock2());
            frame.setSize(300,300);
            frame.setVisible(true);
      }

}

0
Comment
Question by:me_new
  • 3
5 Comments
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11924079
Add the label every time you update it and validate the frame's content pane with frame.getContentPane().validate(). That should do it. :)
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11924090
Also note: label.setText(cal.getTime().toString()) will need to be in a loop or timer. It should look something like this (though this is an infinite loop):
JFrame frame; //Declare frame at the top so it isn't local

while (true)
{
label.setText(cal.getTime().toString());
frame.add(label);
frame.getContentPane().validate();
}
You might want to use a timer instead, firing once every second...
0
 
LVL 3

Expert Comment

by:CI-Ia0s
ID: 11924095
Oh, oops. You're adding the label to the Panel. In that case, just make frame.add(label) (in the above example) into add(label). That should do it...
0
 
LVL 92

Accepted Solution

by:
objects earned 20 total points
ID: 11924179
you don't loop in your thread

change:

  if (mythread==thread)

to

  while (mythread==thread)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11924910
You don't need to call

>>Calendar cal=Calendar.getInstance();

in each loop in run. Just call it once in the ctor
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstswap challenge 20 77
Cipher Configuration on Apache HTTPD 4 62
Java exception bubble up 2 18
Unhandled exception type Exception 18 31
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question