The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

Swing Timers

Hi I have a program that should bring up two JTextFields, one with a phrase in it and the other empty. I want it to then wait 5 seconds to give the user time to enter in the translated answer. Then it should compare the two fields and give a response. This is the code I have now -

The main problem I am having is that I don't know how to say "wait 5 seconds before continuing." It just puts up a new phrase, and compares instantly. Another small issue I am having is when I first run it, both the fields are blank and it doesn't start until the time has passed by once. Any help would be appreciated.

should be placed in your go() method. When you start the timer it will wait the specified time, then run the actionPerformed method to check the answer. If you wish to continue as a loop, place the above code at the end of the actionPerformed method as wellk, and then create and start another timer

Re: Swing Timers

Well I got the program to start looping with Thread.sleep(5000). The problem I am having now is that when the JTextFields come up, I am unable to set my cursor there. It's like grabFocus() just isn't working.

Re: Swing Timers

Originally Posted by Sterzerkmode

Well I got the program to start looping with Thread.sleep(5000). The problem I am having now is that when the JTextFields come up, I am unable to set my cursor there. It's like grabFocus() just isn't working.
.

Did you try my original suggestion? Using Thread.sleep in this fashion means you are locking up the program for 5 seconds (the actionPerformed occurs in the Event Dispatch Thread (EDT) - and because all GUI interaction occurs in the EDT making it sleep means nothing else can happen until it stops sleeping). If you want to do it this way you need to spawn another thread, but that shouldn't be necessary. You just need to update the gui, then start the timer (rather than waiting for the timer to fire before updating the gui)

Re: Swing Timers

Okay well I had to where every x amount of time it would compare, and a new word would put up. But ideally, I'd like to set the amount of time between phrase changes and compare the new words after ten seconds. I tried adding a new listener, but it never compares the to. It will just sit there until it's time to change the phrases and changes them.