CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2010
    Posts
    2

    java clock (Help)

    i've some problem on running the time.. the time doesn't tick when i execute it...

    is it correct to write the code like this? without extending JLabel or Applet, or other keyword...

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

    public class runTimer implements Runnable {

    Thread clockThread = null;
    JFrame f;
    DateFormat dateFormat;
    Date currentDate;
    String lastdate;
    JLabel test;

    public runTimer()
    {
    dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    currentDate = new Date();
    lastdate = dateFormat.format(currentDate);

    test = new JLabel(lastdate);

    f = new JFrame("");
    f.setVisible(true);
    f.setSize(500,500);
    f.getContentPane().add(test);
    }
    public void start()
    {
    if (clockThread == null)
    {
    clockThread = new Thread(this, "Clock");
    clockThread.start();
    }

    }
    public void run() {

    // loop terminates when clockThread is set to null in stop()
    while (Thread.currentThread() == clockThread) {
    test.repaint();
    try {
    clockThread.sleep(1000);
    } catch (InterruptedException e){
    }
    }
    }

    public void paint(Graphics g) {
    test.paintComponents(g);

    currentDate = new Date();
    String today;

    dateFormat.equals(new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"));
    today = dateFormat.format(currentDate);

    g.drawString(today,5,100);
    test = new JLabel(today);

    lastdate = today;
    currentDate = null;
    }

    public static void main(String args[])
    {
    new Thread(new runTimer()).start();
    }
    }

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: java clock (Help)

    If you want to regularly update Swing components from a thread, you should use a Swing Timer or, if you have a lot of background processing to do, a SwingWorker thread, otherwise you will have trouble with the Swing EDT (Event Dispatching Thread) which is trying to control the application GUI (see Concurrency In Swing).

    Usually you would have an application with a GUI created and displayed in the EDT (see HelloWorldSwing). To make your GUI clock tick, you'd start a Swing Timer to regularly call an ActionListener that would update the application's clock data and repaint the clock in the Swing EDT.

    The links I gave have full explanations and links to further details. Check out How To Use Timers, and Using Timers In Swing Applications.

    Time is an excellent teacher; but eventually it kills all its students...
    Anon.
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    May 2010
    Posts
    2

    Re: java clock (Help)

    hey dlorde, thanks for the reply, your guide helps alot..
    i'm still in progress on studying it.. hope it solves my problem...

    waiting for more comments and feedback to help out my problems,

    im just a beginner in java threads...
    trying to learn to complete to my college final year project.. 1more month to go...

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: java clock (Help)

    without extending JLabel or Applet, or other keyword...
    JLabel and Applet are not keywords. They are classes. Keywords are a primitive part of the Java language such as do, if, while, etc

    How is the paint() method in your code called? Is it ever called? It doesn't appear to override the paint() method of any class the is part of a GUI.
    Norm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured