• 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

JOptionPane Timeout

 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can anyone provide a code snippet showing how to have a JOptionPane timeout if the user doesn't respond after a specified period? I assume this would be done via a thread or timer, but can't figure out how to make it work.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I like to work with home-made JDialog classes instead of the JOptionPane things. Needs more work of course but also more freedom. Here is a modal
JDialog that asks YES or NO and you have 30 seconds time to make up your mind:

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To close / hide a dialog through code you have to have a reference to it. Unfortunately, the static methods of JOptionPane do not give you the dialog itself.

Now you can create your own dialog like Ko Wey did, or you can create a JOptionPane, use that to call createDialog, and use that reference to close / hide after 30 seconds. Ko Wey's code shows you how to.

One thing to think about: what do you do when the user closes the dialog manually? Is there a way to cancel the thread / timer? Or is it not important because you might only close the already closed dialog?
And what will be your return value (if any) from the JOptionPane dialog?
(Probably the answers will be quite simple, but just think about it for a while )
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic