splintor
splintor

Reputation: 10154

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

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 is my current code:

JFileChooser chooser = new JFileChooser()
{
    public void approveSelection()
    {
        File selectedFile = getSelectedFile();
        if (selectedFile != null && selectedFile.exists( ) )
        {
            int response = JOptionPane.showConfirmDialog(
                    this,
                    "The file " + selectedFile.getName() + " already exists."
                        + " Do you want to replace the existing file?",
                    getDialogTitle(),
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.WARNING_MESSAGE);
            if (response != JOptionPane.YES_OPTION )
            {
                return;
            }
        }

        super.approveSelection();
    }
};

Upvotes: 30

Views: 47630

Answers (5)

subes
subes

Reputation: 1832

This is my solution:

import java.awt.Component;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.UIManager;


public class NegativeDefaultButtonJOptionPane {

public static int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) {
    List<Object> options = new ArrayList<Object>();
    Object defaultOption;
    switch(optionType){
    case JOptionPane.OK_CANCEL_OPTION:
        options.add(UIManager.getString("OptionPane.okButtonText"));
        options.add(UIManager.getString("OptionPane.cancelButtonText"));
        defaultOption = UIManager.getString("OptionPane.cancelButtonText");
        break;
    case JOptionPane.YES_NO_OPTION:
        options.add(UIManager.getString("OptionPane.yesButtonText"));
        options.add(UIManager.getString("OptionPane.noButtonText"));
        defaultOption = UIManager.getString("OptionPane.noButtonText");
        break;
    case JOptionPane.YES_NO_CANCEL_OPTION:
        options.add(UIManager.getString("OptionPane.yesButtonText"));
        options.add(UIManager.getString("OptionPane.noButtonText"));
        options.add(UIManager.getString("OptionPane.cancelButtonText"));
        defaultOption = UIManager.getString("OptionPane.cancelButtonText");
        break;
        default:
            throw new IllegalArgumentException("Unknown optionType "+optionType);
    }
    return JOptionPane.showOptionDialog(parentComponent, message, title, optionType, JOptionPane.QUESTION_MESSAGE, null, options.toArray(), defaultOption);
}

}

Upvotes: 10

Carlos Tasada
Carlos Tasada

Reputation: 4444

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.

Upvotes: 25

Cristian
Cristian

Reputation: 11

For the above example, it is JOptionPane.showOptionDialog Those arguments can no be passed to showConfirmDialog because it does not have them.

More people might be looking for this so why not offer a "working" solution.

Upvotes: 1

Koen Weyn
Koen Weyn

Reputation: 1004

If you don't want to hardcode "Yes" and "No" (for instance when your app is localized for other languages), you can use UIManager resources:

UIManager.getString("OptionPane.yesButtonText", l)
UIManager.getString("OptionPane.noButtonText", l)

Upvotes: 8

Vinay Sajip
Vinay Sajip

Reputation: 99510

Use this constructor:

JOptionPane(Object message, int messageType, int optionType,
            Icon icon, Object[] options, Object initialValue)

where options specifies the buttons, and have initialValue (one of the options values) specify what the default is.

Update: You can call showOptionDialog rather than showConfirmDialog. The former takes options and initialValue parameters.

Upvotes: 18

Related Questions