thread problem - result is blinking in the textarea

Posted on 2009-02-19
Medium Priority
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
  • 4
LVL 86

Expert Comment

ID: 23683505
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

ID: 23683599
Here's an example:


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


Author Comment

ID: 23683676
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

CEHJ earned 2000 total points
ID: 23683719
>>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

ID: 23683931

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

612 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