14

I had created a JOptionPane of type showInputDialog. When it opens it, it shows me two buttons: OK and Cancel. I would like to handle the action when I push on Cancel button, but I don't know how to reach it. How can I get it?

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Mazzy
  • 13,354
  • 43
  • 126
  • 207

7 Answers7

26

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 {

}

Alternatively 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)) {

}
tenorsax
  • 21,123
  • 9
  • 60
  • 107
  • 1
    Downvoted because this doesn't answer the question - not sure why it's the accepted answer. Mazzy was asking specifically about showInputDialog. – Amber Aug 14 '19 at 16:28
  • @Amber There is an edit from 2012 that shows how to use `showInputDialog`, near the end of the answer. – tenorsax Aug 14 '19 at 18:42
9

This is an old question, and I am an Java newbie so there might be better solutions, but I wanted to know the same, and maybe it can help others, what I did was to check if the response was null.

If the user clicks "cancel", the response will be null. If they click "ok" without entering any text the response will be the empty string.

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)

    }
Amber
  • 2,413
  • 1
  • 15
  • 20
Kjell
  • 91
  • 1
  • 1
  • 1
    But your code does not handle a situation, if you your input is empty and you hit OK button. You get error. – frank17 Apr 02 '16 at 20:57
  • This is the correct answer. If the user doesn't input anything and presses ok, the response will be the empty string. If they hit cancel (even if they have entered text), the response will be null. – Amber Aug 14 '19 at 16:34
6

The showMessageDialog, shouldn't show two buttons, so something is amiss with either your code or your interpretation of it. Regardless, if you want to give the user an choice and want to detect that choice, don't use a showMessageDialog but rather a showConfirmDialog, and get the int returned and test it to see if it is JOptoinPane.OK_OPTION.

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
0

You could do it like this:

String val = JOptionPane.showInputDialog("Value: ");
if(val == null){
  // nothing goes here if yo don't want any action when canceled, or
  // redirect it to a cancel page if needed
}else{
  //insert your code if ok pressed
  // JOptionPane return an String, as you was talking about int
  int value = Integer.ParseInt(val);
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

showInputDialog return NULL if you click cancel and String object even if it's empty. So all you need to do is test if it's Null and if it's not empty.

Adel
  • 5,341
  • 2
  • 21
  • 31
-1
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;
            }
        }
    }
}
Leopold Joy
  • 4,524
  • 4
  • 28
  • 37
-2

This may be your answer:

package Joptionpane;

import javax.swing.JOptionPane;
    
public class Cancle_on_JOptionPane {

    public static void main(String[] args) {
        String s;
        int i;
        // Set the process limit to any number, that use when you need to use for comparability and limited to get out of the program when the answer is correct.(in this case is 100)
        for(i=0;i<100;i++){
        s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
                        // Resolve process by compare answer with character to got the right answer, If it's no character it would show question box until user type answer or click cancel.
                        try{      
                                 if(s.equals(""))  {
                                                            JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
                                                            i=2; // Set i=2 to repeating question box loop until user give answer.
                                                       }
                                 else  {
                                           JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
                                           i=100; // Get out from question box when the variable is not empty.
                                         }
                            }
        catch(Exception e)
              {
                 JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
                 i=100; // Get out from question box when user click cancel and showing "cancel message", You can link to another action here.
              }
     }
   }
}
Athit See
  • 1
  • 1
  • 2
  • While this code block may answer the question, it would be best if you could provide a little explanation for why it does so. Please [edit] your answer to include such a description. – Artjom B. Jul 04 '15 at 10:24