Solved

Auto Update

Posted on 2004-10-24
147 Views
Last Modified: 2010-03-31
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
Question by:Shroder
    5 Comments
     
    LVL 92

    Expert Comment

    by:objects
    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
     
    LVL 92

    Expert Comment

    by:objects
    Another option would be to use the Timer class.
    0
     
    LVL 3

    Expert Comment

    by:RuadRauFlessa
    I would go with what objects said since it does most of the threading work for you.
    0
     
    LVL 86

    Accepted Solution

    by:
    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
     
    LVL 86

    Expert Comment

    by:CEHJ
    8-)
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Suggested Solutions

    Title # Comments Views Activity
    has12 challenge 13 46
    Using java code in .Net/c#: namespace issue 3 32
    zeroMAx challenge 20 54
    scoresClump  challenge 31 70
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
    Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

    845 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now