Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JOptionPane Timeout  RSS feed

 
Adam Richards
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • 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.
 
Ko Wey
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • 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:

 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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 )
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!