• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Time-based responses in a JFrame?

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch Zed Forde.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic