Come rendere JOptionPane.showConfirmDialog hanno alcun selezionata per impostazione predefinita?

StackOverflow https://stackoverflow.com/questions/1395707

  •  22-09-2019
  •  | 
  •  

Domanda

ho implementato una finestra di dialogo Salva in Java che richiede all'utente se il file esiste già, e voglio che l'opzione No a essere selezionata per impostazione predefinita. Come posso fare questo?

Ecco il mio codice corrente:

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();
    }
};
È stato utile?

Soluzione

Utilizzare questo costruttore:

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

dove options specifica i pulsanti, e hanno initialValue (uno dei valori options) specificare cosa il valore predefinito è.

Aggiornamento: È possibile chiamare showOptionDialog piuttosto che showConfirmDialog. Il primo prende parametri options e initialValue.

Altri suggerimenti

Questa è la prima cosa che mi viene in mente.

//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]);

Ma probabilmente c'è un approccio migliore.

Se non si desidera hardcode "Sì" e "No" (ad esempio, quando la vostra applicazione è localizzata in altre lingue), è possibile utilizzare le risorse UIManager:

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

Questa è la mia soluzione:

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

}

Per l'esempio precedente, è JOptionPane.showOptionDialog Tali argomenti non possono essere passati a showConfirmDialog perché non li hanno.

Sempre più persone potrebbero essere alla ricerca di questo e allora perché non offrire una soluzione "di lavoro".

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top