Search code examples
javaswingawtjtextfield

Update my textField every 5 seconds


I'm just starting with Java and I need help please, I want to update my JtextField every 5 seconds, I searched something and I tried with thread.sleep(5000) but its not working (and i don't know why). Here is the code of my JtextField:

    textField_1 = new JTextField();
    textField_1.setText("0656");
    textField_1.setFont(new Font("Verdana", Font.PLAIN, 80));
    textField_1.setToolTipText("");
    textField_1.setHorizontalAlignment(SwingConstants.CENTER);
    textField_1.setBounds(212, 120, 600, 150);
    frame.getContentPane().add(textField_1);
    textField_1.setColumns(10);

Solution

  • The easiest way to achieve this is using class Timer.

        Timer t = new Timer();
        t.schedule(new TimerTask() {
            @Override public void run() {
                  // textField_t.setText(YOUR TEXT); 
            }
        }, 0L, 5000L);