wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes help with joption pane cancel button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "help with joption pane cancel button" Watch "help with joption pane cancel button" New topic
Author

help with joption pane cancel button

Kendra Payne
Greenhorn

Joined: Oct 17, 2006
Posts: 22
I am almost there with this one!
The book tells me to use a while loop unless the user clicks the cancel button but does not tell me what what code to enter to end the program if they do hit cancel button. I need to end the program at that point but don't know how. Is this the action listener? I am confused. Please help-Thanks

Mark Van Tuyl
Ranch Hand

Joined: Mar 22, 2002
Posts: 60


<a href="http://www.catb.org/~esr/faqs/smart-questions.html" target="_blank" rel="nofollow">How To Ask Smart Questions</a>
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the way you have your program now, 'cancel' will crash the program

when using a showInputDialog(), any of 'cancel', 'esc' or 'X' (top right corner)
will return null. your next line will then try to parse a null object

perhaps the easiest way to handle it is to check for null

String strChoice = JOptionPane.showInputDialog(...);
if(strChoice == null) break;//<-------------------------break out of the 'while'
choice = Integer.parseInt(strChoice);


[EDIT]
just re-read this and now noticed the try/catch, so it won't crash, but
checking for null would still be a simple way to handle it
[ October 17, 2006: Message edited by: Michael Dunn ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: help with joption pane cancel button