Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / March 2007

Tip: Looking for answers? Try searching our database.

Date object

Thread view: 
Sagar - 08 Mar 2007 09:32 GMT
hi
friends.
I HAD CREATED ONE APPLET WHICH SHOW A CURRENT TIME.

IN
public void paint(Graphics g){

for(;;){
Thread.sleep(100);
//created one date object

Date d=new Date();

String s=d.toString();

g.drawString(s,10,10);

}

To show time i make thread to sleep for some time.
will date object will create for each time.

if so what to do with it.
SadRed - 08 Mar 2007 10:17 GMT
> hi
> friends.
[quoted text clipped - 19 lines]
>
> if so what to do with it.

Your current code does not make thread except application's main
thread.

Java GUI runs as a single thread. If you do a long and time consuming
task, such as your for(;;) loop, your single app thread is fully
occupied by the task and GUI can't have its turn to run.

Long and time consuming task should be run as a new separate thread.
Try this code:
------------------------------------
/*
<applet code="Sagar" width="200" height="50"></applet>
*/
import java.applet.*;
import java.util.*;
import java.awt.*;

public class Sagar extends Applet{
 String ds;

 public void start(){
   new Thread(){
     public void run(){
       while (true){
         ds = (new Date()).toString();
         repaint();
         try{
           Thread.sleep(1000);
         }
         catch (InterruptedException e){
           e.printStackTrace();
         }
       }
     }
   }.start();
 }

 public void paint(Graphics g){
   g.drawString(ds, 10, 10);
 }
}
--------------------------------------


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.