Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use canceled button in JOptionPane  RSS feed

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • 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
 
Ralph Cook
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!