• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

How to use canceled button in JOptionPane

 
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am using JOptionPane to get an int value for a switch statement, but i can't figure out how i can see if someone clicks the "cancel" option.
I looked in the API for it but i still can't figure it out!

Here is the snippet where i need to use it




any help would be greatly appreciated.

Thanks
Mike
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doing a quick web search, it appears that clicking "Cancel" causes the input dialog to return null instead of a String. You'll have to get the string returned by the dialog and test it for null to do what you want. It might be a good idea anyway, just in case the user types "two", so you can control the error message he gets and try again.

rc
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ralph,

I have read that also in the API and other sources, but my problem is that i am using "choice = Integer.ParseInt" if i were just using JOptionPane and looking for a string then it is simple to see if the input is null.
Or do i need to write an extra method just to use the cancel button?


Mike
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

OK i got it fixed ;) thanks for the tipp Ralph it got me thinking in the right direction!

Here is how i fixed it for a reference for anyone else having this problem.




Mike
 
Marshal
Posts: 80493
455
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about one of the other kinds of dialog for JOptionPane? Or you can pass YES_NO_OPTION, or YES_NO_CANCEL_OPTION. There should be something in the Java™ Tutorials.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:What about one of the other kinds of dialog for JOptionPane? Or you can pass YES_NO_OPTION, or YES_NO_CANCEL_OPTION. There should be something in the Java™ Tutorials.




Thanks Campbell, I know there are other JOptionPane methods and will check those out as well in time. My last posted code works fine now and the reason i wanted to fix that as is was is
because i had the entire program written already and then thought i should utilize the cancel button too although i had a case method to quit the program.



Mike
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic