Link to home
Start Free TrialLog in
Avatar of mordauth
mordauthFlag for United States of America

asked on

thread problem - result is blinking in the textarea

Hi,
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.
Thanks,

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();
        frame.setVisible(true);
 
    }
}
 
/**
 * digitalClockFrame() is used to set a GUI interface
 */
    class digitalClockFrame extends JFrame{
 
        //constructor
        public digitalClockFrame() {
            setTitle("Enchanced Digital Clock");
            setSize(500,300);
            setResizable(false);
            Container appWindow = getContentPane();
            JPanel clockPanel = new JPanel();
            appWindow.add(clockPanel, "Center");
            appWindow.setBackground(Color.lightGray);
            appWindow.setFont(new Font("Monospaced", Font.ITALIC,12));
 
            //start thread
            digitalClockInside digClock = new digitalClockInside(clockPanel);
            digClock.start();
 
 
          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;
 
    //constructor
        public digitalClockInside(JPanel dArea) {
            displayArea = dArea;
            dArea.add(txtShow);
         }
        
        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
         
        txtShow.setFont(font);
        //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 + " " +
                mth.convertDayofWeek(dayOfWeek));
                    
        }// end paint
    
        
       public void run(){
           try {
               for (int i = 1; i <=10000; i ++ )
                   paint();
               sleep(1000);
 
           } 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",
            "December"};
 
        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

Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Here's an example:

http://rabbitbrush.frazmtn.com/AnalogClock.java

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

Avatar of mordauth

ASKER

If I use a Swing Timer, I do not have to use a thread then ?
Where exactly I should add the timer ?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)