automatic update jtextfield

后端 未结 1 423
时光取名叫无心
时光取名叫无心 2021-01-27 07:32

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

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-27 08:28

    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
           */
       }
     }
    

    0 讨论(0)
提交回复
热议问题