Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have made a small java swing application. And i am showing a count and date.
I want this to refresh after 5 sec.
i have used thread for this.

But it happens that thread sleep and did not invoke again. Can any one please suggest me the way.

Code :
Thread t = new Thread(new DBCount(), "thread 1");
t.start();
for (;;) {
Date date = new Date();
DateFormat nyDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
DateFormat.getDateInstance();
TimeZone tz = TimeZone.getTimeZone("America/New_York");         
	       
String finaldate = (nyDf.format(date).replace("EDT", "EST"));
System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL ).format(date));
id = new DBCount().readQuery(con);
prcs = new DBCount().workerprocess(con);
logger.info(finaldate + ": count - " + id);
labelchange(id, prcs,finaldate, con);
t.sleep(5000);			
}
Posted
Updated 13-Nov-13 6:39am
v2

In this sample program ,able to show time in status bar using timer.Which invokes the thread of swing .I think this concept may help you to solve the problem.

www.set.up2java.com/2013/09/developing-status-bar-component-in-java.html
 
Share this answer
 
the reason is your thread has been executed, and you didn't specify any code block inside. If you want to make the thread sleep, you should write code like:
Java
Thread thread = new Thread(new Runnable(){

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (;;) {
                Date date = new Date();
                DateFormat nyDf = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
                DateFormat.getDateInstance();
                TimeZone tz = TimeZone.getTimeZone("America/New_York");

                String finaldate = (nyDf.format(date).replace("EDT", "EST"));
                System.out.println(DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL ).format(date));
                id = new DBCount().readQuery(con);
                prcs = new DBCount().workerprocess(con);
                logger.info(finaldate + ": count - " + id);
                labelchange(id, prcs,finaldate, con);
                t.sleep(5000);
                }
    }

});
thread.start();
 
Share this answer
 
Comments
master77777 21-Nov-13 2:19am    
Hi Xiao Ling
I implemented this code but on system lock and when again i unlock system , The id and prcs which is i am displaying in a label stop querying to DB. But time is displaying fine.
Please suggest.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900