1

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);
CoderNeji
  • 2,056
  • 3
  • 20
  • 31
marcss
  • 253
  • 2
  • 14

3 Answers3

3

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);
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • But that will show the value of the textField every 5 seconds, I only want to show the value for 5 seconds every time that the value were updated. – marcss Apr 16 '15 at 07:28
  • This is not what you asked in your original post. Do you mean that you want to show value into text field and then clear the text field? Kind of blinking? – AlexR Apr 16 '15 at 07:42
  • Sorry it was a mistake, Yes, I want to show the IF ("0656") for 5 second and then nothing until the value is updated – marcss Apr 16 '15 at 07:44
  • What does it mean "and then nothing?" Remove the value? – AlexR Apr 16 '15 at 07:46
  • 1
    You can create 2 timers: one that shows value every 5 seconds, second that removes value every 5 seconds. The second timer's delay is 5 seconds also (instead of 0L). – AlexR Apr 16 '15 at 07:50
  • I tryed with your code copyed two times and it doesn't work, it's only show the latest timer made – marcss Apr 16 '15 at 08:00
  • Have you changed the "delay" of the second timer? – AlexR Apr 16 '15 at 08:01
  • This would update the text field outside the event dispatch thread. It's better to use a swing [Timer](https://docs.oracle.com/javase/tutorial/uiswing/misc/timer.html) instead of java.util.Timer. – kiheru Apr 16 '15 at 08:09
  • It works, but for 1 milisecond I can see how change the "0656" for the "", but no problem, now, I'm trying to use the same timers for show the date and update every second: Date fecha=new java.util.Date(); textField = new JTextField(fecha.toLocaleString()); textField.setFont(new Font("Verdana", Font.BOLD, 15)); textField.setHorizontalAlignment(SwingConstants.CENTER); textField.setBackground(UIManager.getColor("Button.textField.setBounds(362, 665, 300, 50); frame.getContentPane().add(textField); textField.setColumns(10); – marcss Apr 16 '15 at 08:21
1

Use Swing Timer component for repetitive tasks with Swing GUI toolkit:

ActionListener task = new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                // Do stuff
            }
};
Timer timer = new Timer(100 ,task); // Execute task each 100 miliseconds
timer.setRepeats(true);
timer.start();
Branislav Lazic
  • 14,388
  • 8
  • 60
  • 85
1

Just remember that the 5000 is 5000 milliseconds and 1000 milliseconds is equal to 1 second.

 `javax.swing.Timer

  final Timer updater = new Timer(5000, new ActionListener() {
  public void actionPerformed(ActionEvent e) 
  {
  // update JTextField
  }
  });
  JButton button = new JButton("Start");
  button.addActionListener(new ActionListener() {

  public void actionPerformed(ActionEvent e) {
  updater.start();
  }
  });`
BNats
  • 34
  • 8