• 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
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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: 76870
366
  • 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
 
Never trust an airline that limits their passengers to one carry on iguana. Put this tiny ad in your shoe:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic