How to handle cancel button in JOptionPane

こ雲淡風輕ζ 提交于 2019-12-03 12:15:39

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

}

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.

Kjell

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)

    }
hamooda aburezk
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;
            }
        }
    }
}
emendescv

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);
}
Athit See

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;
        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;
              }
     }
   }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!