1

How do I go about making a timer that will increment the time in a JFormattedTextField?

davidahines
  • 3,976
  • 16
  • 53
  • 87

1 Answers1

4

Look at http://download.oracle.com/javase/6/docs/api/javax/swing/Timer.html

This lets you execute an ActionListener repeatedly in the Swing event thread with a specified delay.

The just get and set the value from the textfield.

Edit: not a Runnable but an ActionListener

Reverend Gonzo
  • 39,701
  • 6
  • 59
  • 77
  • Speedy response. That probably means I was googling poorly. Thanks. – davidahines May 04 '11 at 18:19
  • Would it be accurate to do a Thread.sleep for (1000) and then just have it update the field with a second added? – davidahines May 04 '11 at 18:22
  • 2
    No, you generally don't want to use a Thread.sleep() as that will block your UI in the meantime. If you just want to call it once, call timer.setRepeats(false). – Reverend Gonzo May 04 '11 at 18:30
  • Ahh. I just read that link and it's simple and seems to be perfect, thanks. – davidahines May 04 '11 at 19:00
  • 1
    +1 `javax.swing.Timer` is a good choice for this. See also this [example](http://stackoverflow.com/questions/5528939/java-swing-two-classes-where-to-put-if-statements-and-new-actionlisteners/5529043#5529043). – trashgod May 04 '11 at 20:41