Pergunta

I am trying to implement a "Save As" dialogue using a JFileChooser. The dialogue is supposed to give the user the ability to type a filename and click save, at which point the new File object will be returned and created.

This works, but I am running into a problem when I try to add another dialogue. Specifically, I want to create a "File already exists" dialogue using a JOptionPane to warn the user if they are trying to create a file with the same name as a pre-existing file (this is a common feature in many programs). The dialogue prompts "File <filename> already exists. Would you like to replace it?" (Yes/No). If the user selects "yes", the file object should be returned as normal. If the user selects "no", the JFileChooser should stay open and await the selection/creation of another file.

The problem is that I can't find a way to cancel the selection (if the user chooses "no") and keep the dialogue open. I have the code:

public void saveAs()
{
    if (editors.getTabCount() == 0)
    {
        return;
    }

    final JFileChooser chooser = new JFileChooser();

    chooser.setMultiSelectionEnabled(false);

    chooser.addActionListener(new ActionListener()
    {
        public void actionPerformed(ActionEvent arg0)
        {
            File f =  chooser.getSelectedFile();

            if (f.exists())
            {
                int r = JOptionPane.showConfirmDialog(
                     chooser,
                     "File \"" + f.getName() +
                     "\" already exists.\nWould you like to replace it?",
                     "",
                     JOptionPane.YES_NO_OPTION);    

                //if the user does not want to overwrite
                if (r == JOptionPane.NO_OPTION)
                {
                    //cancel the selection of the current file
                    chooser.setSelectedFile(null);
                }
            }
        }
    });

    chooser.showSaveDialog(this);

    System.out.println(chooser.getSelectedFile());
}

This successfully cancels the selection of the file if the user selects "no" (that is the selected file upon the closure of the dialogue is null). However, it also closes the dialogue immediately afterwards.

I would prefer it if the dialogue remained open when this happened. Is there any way to do this?

Foi útil?

Solução

Override the approveSelection() method. Something like:

JFileChooser chooser = new JFileChooser( new File(".") )
{
    public void approveSelection()
    {
        if (getSelectedFile().exists())
        {
            System.out.println("Do You Want to Overwrite File?");
            // Display JOptionPane here.  
            // if yes, super.approveSelection()
        }
        else
            super.approveSelection();
    }
};

Outras dicas

Maybe you can simple reopen the JFileChooser again if they don't wanna overwrite the file.

The dialog will have the same directory as before, so the user no need to navigate again.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top