We help IT Professionals succeed at work.

thread problem - result is blinking in the textarea

mordauth
mordauth asked
on
Medium Priority
312 Views
Last Modified: 2013-11-23
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

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
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
CERTIFIED EXPERT
Top Expert 2016

Commented:
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

Author

Commented:
If I use a Swing Timer, I do not have to use a thread then ?
Where exactly I should add the timer ?
CERTIFIED EXPERT
Top Expert 2016
Commented:
>>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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
CERTIFIED EXPERT
Top Expert 2016

Commented:
:-)
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.