Meaningless Drivel is fun!
The moose likes Swing / AWT / SWT and the fly likes Code for JOptionPane buttons Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Code for JOptionPane buttons" Watch "Code for JOptionPane buttons" New topic

Code for JOptionPane buttons

Sandeep Swaminathan
Ranch Hand

Joined: Apr 23, 2008
Posts: 52
How should one write code for the the YES, NO and CANCEL buttons of showConfirmDialog() of JOptionPane. Like if its YES then one must delete all the info from the database, NO then don't delete and CANCEL to go back!!

Music is all around us. All you have to do is just LISTEN to it,
Sandeep Swaminathan
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10825

The showConfirmDialog returns an int which represents the user's choice. You typically trap the return value and process accordingly.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
when you check the return values, check them against JOptionPane.YES_OPTION (instead of == 0)
you also have to check for the user clicking 'X' or 'esc'

do you really need both "NO then don't delete and CANCEL to go back!!"
why not just YES_NO or OK_CANCEL
Sandeep Swaminathan
Ranch Hand

Joined: Apr 23, 2008
Posts: 52
hey Michael n Maneesh,

Thanks a lot! That really helped me! And ya I don't need them both! Yes or No is more than enough :p
I agree. Here's the link:
subject: Code for JOptionPane buttons
jQuery in Action, 3rd edition