Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7085615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:26:47+00:00 2026-05-28T07:26:47+00:00

I am trying to create a digital clock using a Thread as this seems

  • 0

I am trying to create a digital clock using a Thread as this seems to me the logical way one would do it.
I am not sure if I am going about it the right way but what I had in mind is to create the initial current System time using the JFrame constructor and display it as text using a label. In the constructor I then create the thread object with which to update the time.

Struggling a bit and was hoping for some advice as to how to do it right.

setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
        setBounds(50, 50, 200, 200);

        JPanel pane = new JPanel();
        label = new JLabel();

        //Font localTime = new Font("Lumina", Font.BOLD , 24);

        pane.add(label);
        add(pane);
        sdf = new SimpleDateFormat("HH:mm:ss");
        date = new Date();

        s = sdf.format(date);
        label.setText(s);
        setVisible(true);
        runner = new Thread(this);

        while(runner == null)
        {
            runner = new Thread(this);
            runner.start();

        }

This is then my run() method to update the clock every second.

public void run()
{
    while(true)
    {
        try
        {
            Thread.sleep(1000);
            sdf = new SimpleDateFormat("HH:mm:ss");
            date = new Date();
            s = sdf.format(date);
            label.setText(s);
        }
        catch(Exception e){}

    }

Main method.

public static void main(String[] args)
{
    new DigitalClock().setVisible(true);


}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T07:26:48+00:00Added an answer on May 28, 2026 at 7:26 am

    The label state should be updated in the Event Dispatch Thread.

    You need to add the following modification:

        SwingUtilities.invokeLater(new Runnable() {
    
            @Override
            public void run() {
                label.setText(s);
            }
        });
    

    instead of simply updating the label from the separate thread.

    It’s worth to have a look at the simple description of The Swing GUI Freezing Problem and it’s simple solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a digital clock display using 7 segment displays. I can
I'm trying create a small http proxy service. This is not working so well.
I'm trying to create a digital clock widget with custom typeface font. I got
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create my first iPhone app that would play back audio. When I
Trying to create several layers of folders at once C:\pie\applepie\recipies\ without using several different
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I am trying to create an application which reads data from digital compass. I
All, I am trying create a 'to & fro' animation using jquery animate &

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.