• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

help with joption pane cancel button

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic