aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Time-based responses in a JFrame? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Time-based responses in a JFrame?" Watch "Time-based responses in a JFrame?" New topic
Author

Time-based responses in a JFrame?

Zed Forde
Greenhorn

Joined: Nov 05, 2010
Posts: 1
I've been trying to puzzle this out as part of a larger project I'm working on: I'm trying to set up a JFrame that will react to either the passage of time or a click.

What I want it to do is to have my program wait a short moment for a click, or after a short time move on anyway. The simplest way to explain what I'm trying to do is probably look at it as if it were a reaction test--start the test, wait a couple of seconds. If the user clicks in time, tell him how much time elapsed. If he doesn't after a second or two, don't keep waiting--terminate that and instead say "Too slow!"

However, I can't do that--if I just set up my JFrame normally, it'll hang, waiting for a click. As a last resort, I could get the system time, wait for the click (however long it takes), and then compare it to the ending system time, but I'd really like things to continue happening without needing that input.

Someone I know casually suggested using either Timer or TimerTask to do what I want to, but I couldn't parse how they work and I don't see him around a computer to get help--if those can do what I want, can you please point me at a good example or tutorial for them, beyond the download.oracle reference piece?
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1824
    
    7

Like you already mentioned, a timer mechansim is what you're probably looking for here.
The most obvious one would be javax.swing.Timer, for which there's a nice little tutorial at the Oracle website. You could also use java.util.Timer or some other implementation, but an important benefit you get for free using javax.swing.Timer is the fact that it automagically executes tasks on the EDT. If you don't know what that means or why that it important, you should have a look at this tutorial first.

I'll move this to our Swing / AWT / SWT / JFace forum. If you have any further questions or run into difficulties, that's where you'll most likely find the answers you need


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
I second Jelle's recommendation that this would be easy to do with a Swing Timer, and perhaps you should consider displaying the information in a JDialog rather than a JFrame since it sounds more dialog-ish to me (though I could be wrong).

Oh, and also, Welcome to the Ranch!!
Alex Hurtt
Ranch Hand

Joined: Oct 26, 2010
Posts: 98
Wouldn't he want to NOT execute the Timer on the EDT though? I'm noodling this through in my head and I have only minimal experience working with Swing but...would the Timer executing on the EDT block the GUI preventing it from detecting the click event until such time as the timer expires? If that's the case you probably actually want to execute the Timer task on a separate thread, not on the EDT. In that case there is also a utility called SwingWorker which is designed to help execute background tasks such as this.
pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
Alex Hurtt wrote:Wouldn't he want to NOT execute the Timer on the EDT though? I'm noodling this through in my head and I have only minimal experience working with Swing but...would the Timer executing on the EDT block the GUI preventing it from detecting the click event until such time as the timer expires? If that's the case you probably actually want to execute the Timer task on a separate thread, not on the EDT. In that case there is also a utility called SwingWorker which is designed to help execute background tasks such as this.


It's just the opposite. The Timer doesn't do its counting on the EDT but rather uses a background thread for this. What it does do, however is guarantee that the code present in the Timer ActionListener's actionPerformed method, the code that is tripped by the timer is done on the EDT. Please read the Swing tutorial on the Swing Timer and you'll see all this explained.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39877
    
  28
Welcome to the Ranch Zed Forde.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Time-based responses in a JFrame?