How to make JOptionPane.showConfirmDialog have No selected by default?

前端 未结 5 736
感动是毒
感动是毒 2020-12-11 00:04

I implemented a Save As dialog in Java that prompts the user if the file already exists, and I want the No option to be selected by default. How do I do this?

Here i

5条回答
  •  醉话见心
    2020-12-11 00:53

    That's the first thing that comes to my mind.

    //Custom button text
    Object[] options = {"Yes",
                        "No"};
    JOptionPane.showOptionDialog(this, "The file " + selectedFile.getName() + 
                      " already exists. Do you want to replace the existing file?", 
                      getDialogTitle(), 
                      JOptionPane.YES_NO_OPTION, 
                      JOptionPane.WARNING_MESSAGE, 
                      null, options, options[1]);
    

    But probably there's a better approach.

提交回复
热议问题