I'm trying to write an
applet that gets information from a file every 30 seconds and displays it to a series of labels. My code looks like this:
public class App extends Applet{
Label lblTimeStamp;
WeatherData data;
String path = "update.dat";
public void init(){
resize(300, 800);
setLayout(new FlowLayout());
setBackground(Color.black);
lblTimeStamp = new Label("lblTimeStamp");
lblTimeStamp.setAlignment(Label.CENTER);
lblTimeStamp.setFont(new Font("Serif", Font.BOLD, 12));
lblTimeStamp.setForeground(Color.red);
while(true){
try{
Thread.sleep(30000);
FileInputStream fs = new FileInputStream(path);
ObjectInputStream is = new ObjectInputStream(fs);
data = (WeatherData)is.readObject();
lblTimeStamp.setText(data.getTimeStamp().format(new Date()));
os.close();
}
catch(InterruptedException e){
}
catch(Exception err){
System.out.println(err);
}
}
}
}
I have a class defined called WeatherData that has the values that it's looking for. My problem is the point at which the Thread.sleep() is issued. I've tried moving it out of the while loop, and when the applet is run, it executes the Thread.sleep() statement before any other statement, except the resize(). No matter where I put Thread.sleep(), it runs first. When it is in the while loop, it executes once, then nothing else happens. Can anyone explain this to me? Thanks!
Brett Fitzgerald