How to handle cancel button in JOptionPane

I created a JOptionPane type showInputDialog . When he opens it, he shows me two buttons: OK and Cancel . I would like to handle the action when I click the Cancel button, but I do not know how to achieve it. How can i get it?

+8
java swing jbutton joptionpane
source share
5 answers

For example:

 int n = JOptionPane.showConfirmDialog( frame, "Would you like green eggs and ham?", "An Inane Question", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) { } else if (n == JOptionPane.NO_OPTION) { } else { } 

Alternative with showOptionDialog :

 Object[] options = {"Yes, please", "No way!"}; int n = JOptionPane.showOptionDialog(frame, "Would you like green eggs and ham?", "A Silly Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (n == JOptionPane.YES_OPTION) { } else if (n == JOptionPane.NO_OPTION) { } else { } 

See How to make dialogs for more details.

EDIT: showInputDialog

 String response = JOptionPane.showInputDialog(owner, "Input:", ""); if ((response != null) && (response.length() > 0)) { } 
+21
source share

Two buttons should not be displayed in ShowMessageDialog, so something is wrong with your code or your interpretation. Regardless if you want to give the user a choice and want to define that choice, do not use showMessageDialog, but rather showConfirmDialog, and return an int and check it if it is JOptoinPane.OK_OPTION.

+6
source share

This is an old question, and I'm new to Java, so there might be better solutions, but I would like to know the same thing, and maybe this can help others that I did to check if the answer was empty.

This worked for me:

 //inputdialog JOptionPane inpOption = new JOptionPane(); //Shows a inputdialog String strDialogResponse = inpOption.showInputDialog("Enter a number: "); //if OK is pushed then (if not strDialogResponse is null) if (strDialogResponse != null){ (Code to do something if the user push OK) } //If cancel button is pressed else{ (Code to do something if the user push Cancel) } 
+1
source share
 package Joptionpane; import javax.swing.JOptionPane; public class Cancle_on_JOptionPane { public static void main(String[] args) { String s; int i; for (i=0;i<100;i++){ s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); try { if (s.equals("")) { JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=2; } else { JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } catch (Exception e) { JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } } } 
0
source share

This could be your answer:

 package Joptionpane; import javax.swing.JOptionPane; public class Cancle_on_JOptionPane { public static void main(String[] args) { String s; int i; for(i=0;i<100;i++){ s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); try{ if(s.equals("")) { JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=2; } else { JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } catch(Exception e) { JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } } } 
-one
source share

All Articles