automatic update jtextfield

Does anyone know how to update a jTextfield every 5 secondes? Automatic. So no userinput is required. It is used to update time in a textfield. This is what i tried but then my program freezes.

while(true){
                        Thread.sleep(1000);
                        txtCheck.setText(convert.getCheck());
                        System.out.println("Ja");
                        }

convert is a thread, i tried to throw an exception but failed, cause Eclise says Threads can't throw exceptions.

Convert.getCheck:

    public String getCheck() {
        return check;
    }

Answers


You want to use a Swing Timer object. Here is a Oracle tutorial

To start with, you need to have your class implement the ActionListener interface. Inside of your class, you also need to implement an actionPerformed() method. Finally, you should start the timer in your main() function or somewhere

timer = new Timer(5000, MyClass);
timer.setInitialDelay(pause);
timer.start();

You would then implement your class like:

public class MyClass implements ActionListener
{
   ...

   void actionPerformed(ActionEvent e) {
       /*
        This is called every time the timer fires, put your code here
       */
   }
 }

Need Your Help

Classes calling a common method - delegate?

c# multithreading class

I have a form which creates a BackgroundWorker. The worker then does some work, and does a ReportProgress to send messages back to the UI while it's busy. I then log those messages to the UI. The

xPages - session.getDatabase not returning value

javascript xpages xpages-ssjs

I have a test database on my PC that has a drop-down field listing all our clients. I'm now trying to redirect it to the database containing this information on the server. (I load it into a session

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.