• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

Auto Update

I am creating a java applet where it needs to show current data at x intervals. So I'm looking for a way to update the displayed data every 5 or 10 seconds. The data is displayed with both text and images. I have tried several things but seem to keep running into dead ends. I'm trying to find the best way to accomplish this. Any information or references would be appreciated.
0
Shroder
Asked:
Shroder
  • 2
  • 2
1 Solution
 
objectsCommented:
Make you applet runnable and use a run() method something like the following:


public void run()
{
   while (running)
   {
   try
   {
      Thread.sleep(5000);
     
      // do what u need to do
   }
   catch (Exception ex)
   {
      ex.printStackTrace();
   }
   }
}
0
 
objectsCommented:
Another option would be to use the Timer class.
0
 
RuadRauFlessaCommented:
I would go with what objects said since it does most of the threading work for you.
0
 
CEHJCommented:
If you're using Swing components in your applet, you should use a Swing Timer. Here's a clock example you can run:

/*
<applet codebase="." code="ClockApplet.class" width="300" height="100">
</applet>
*/

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ClockApplet extends JApplet implements ActionListener {
      Timer timer;
      Date currentTime;
      SimpleDateFormat timeFormat;
      JLabel labTime;

      public void init() {
            labTime = new JLabel();
            setLayout(new FlowLayout());
            currentTime = new Date();
            timeFormat = new SimpleDateFormat("HH:mm:ss");
            labTime.setText(timeFormat.format(new Date()));
            add(labTime, SwingConstants.CENTER);
            timer = new Timer(1000, this);
            timer.start();
      }

      public void actionPerformed(ActionEvent e) {
            labTime.setText(timeFormat.format(new Date()));
      }
}
0
 
CEHJCommented:
8-)
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now