thread problem - result is blinking in the textarea

Posted on 2009-02-19
Last Modified: 2013-11-23
I am writing an application which shows a current time, month, year and month.
Since it shows the time, it have to refresh every second.
The program works; however, while it displays the frame it is constantly blinking.
I think the problem is with a loop and thread but I am not sure where exactly.
Below is my code.
I appreciate any help.

package digitalclock;

import java.awt.event.*;

import java.awt.*;

import javax.swing.*;

import java.util.Calendar;

public class Main {

    public static void main(String[] args) {

        JFrame frame = new digitalClockFrame();





 * digitalClockFrame() is used to set a GUI interface


    class digitalClockFrame extends JFrame{


        public digitalClockFrame() {

            setTitle("Enchanced Digital Clock");



            Container appWindow = getContentPane();

            JPanel clockPanel = new JPanel();

            appWindow.add(clockPanel, "Center");


            appWindow.setFont(new Font("Monospaced", Font.ITALIC,12));

            //start thread

            digitalClockInside digClock = new digitalClockInside(clockPanel);


          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // close the frame

        } // enddigitalClockFrame()

    }//end digitalClockFrame() extends Jframe

 class digitalClockInside extends Thread{

    private JPanel displayArea;

    TextArea txtShow = new TextArea(10, 30);

    protected  Font font = new Font("Monospaced", Font.BOLD,16);

    protected Color color   = Color.BLACK;


        public digitalClockInside(JPanel dArea) {

            displayArea = dArea;




        public void paint(){

        Calendar calendar = Calendar.getInstance();

        int hour = calendar.get(Calendar.HOUR);

        int hrAmPm = calendar.get(Calendar.AM_PM);

        int minute = calendar.get(Calendar.MINUTE);

        int second = calendar.get(Calendar.SECOND);

        int month = calendar.get(Calendar.MONTH);

        int year = calendar.get(Calendar.YEAR);

        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK);

        int day = calendar.get(Calendar.DATE); // day of month



        //convert month int value to string

        dateConvertor mth = new dateConvertor();


        txtShow.setText("\n\t"+ "   " +hour + ":" +  minute/10 +minute%10 + ":" + second/10 + second%10 + " "

                + mth.convertAmPM(hrAmPm ) );

        txtShow.append("\n\t" +"   " + day +" " + mth.convertMonth(month) + " " + year + " " +



        }// end paint



       public void run(){

           try {

               for (int i = 1; i <=10000; i ++ )



           } catch (Exception e) {



}// end digitalClockInside 

//----------------------------------------------second class to handle conversion -------------

package digitalclock;

import java.util.Calendar;

public class dateConvertor {


    static public String convertMonth(int calMonth){

        String monthName = "";

        String[] month = {"January", "February",

            "March", "April", "May", "June", "July",

            "August", "September", "October", "November",


        monthName = month[calMonth];

        return monthName;


    static public String convertDayofWeek (int calDay){

        String dayName = "";

        String[] dayOfWeek = {"Sat","Sun", "Mon", "Tue","Wed","Thu","Fri"};

         dayName = dayOfWeek[calDay];

         return dayName;


    static public String convertAmPM (int calAmPm){

        String dayTime = "";

        if (calAmPm == 0) {

            dayTime = "AM";

        }else {

            dayTime = "PM";


        return dayTime;



Open in new window

Question by:mordauth
    LVL 86

    Expert Comment

    You need to update the gui on the event dispatch thread. Use a Swing Timer. Set a label to the new text on the timer firing
    LVL 86

    Expert Comment

    Here's an example:

    You of course can replace that call to repaint with a simple call to setText on a JLabel

    LVL 1

    Author Comment

    If I use a Swing Timer, I do not have to use a thread then ?
    Where exactly I should add the timer ?
    LVL 86

    Accepted Solution

    >>I do not have to use a thread then ?

    No - the Timer *is* a thread. Have a look at the example i posted: you can do the same
    LVL 86

    Expert Comment


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    PGP Decryption code using Bouncy Castle jars 11 71
    network + 7 59
    strDist challenge 35 70
    word0 challenge 3 33
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
    This video teaches viewers about errors in exception handling.
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now