1

I have a function that should get the path of a file typed in the textinput of a jfilechooser and pass it to a String. The problem is that I want to check for overwrite if the file exists already. I do have a clue about how to do this, my problem, though, is that, if answering no to the JOptionPane, the JFileChooser closes anyway, because the save button has already been actioned. Now, what I need is that if the answer is no, the program returns to the JFileChooser, still prompting for a name.

Please note that I am searching for an efficient solution, I've already considered executing the function again, but since my program is quite a large one, this way of solving things would cost time and would not be efficient.

Here is the code of my function, yet not completed because I don't know how to handle it.

`public String FileSavePath()throws NullPointerException
    {
        File f=null;
        String theFilepath=null;
        JFileChooser FileChooser = new JFileChooser();
        if(FileChooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION)
        {
            theFilepath=FileChooser.getSelectedFile().getAbsolutePath();
            f=FileChooser.getSelectedFile();
            //System.out.println(theFile);
            if(f.exists())
            {
                int result = JOptionPane.showConfirmDialog(this,"The file exists, overwrite?",
                        "Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
                if(result==JOptionPane.YES_OPTION)
                           {
                   return theFilepath;

                  }
          else // here is what I should do if the user answers 'no' or cancels/closes the JOptionPane
        }
        else return null;
        return theFilepath;

    }`
Mihai Bujanca
  • 4,089
  • 10
  • 43
  • 84
  • This is an old question, but it was the first in my search result. A rather beautiful solution can be found at: https://stackoverflow.com/a/3729157/589525 – Jaap D Sep 13 '19 at 15:23

1 Answers1

2

You need to place your query into a loop until such time as the user can provide you with an acceptable response...

public String FileSavePath() throws NullPointerException {

    boolean acceptable = false;
    String theFilepath = null;

    do {
        theFilepath = null
        File f = null;
        JFileChooser FileChooser = new JFileChooser();
        if (FileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
            theFilepath = FileChooser.getSelectedFile().getAbsolutePath();
            f = FileChooser.getSelectedFile();
            //System.out.println(theFile);
            if (f.exists()) {
                int result = JOptionPane.showConfirmDialog(this, "The file exists, overwrite?",
                        "Existing file", JOptionPane.YES_NO_CANCEL_OPTION);
                if (result == JOptionPane.YES_OPTION) {
                    acceptable = true;
                }
            } else {
                acceptable = true;
            }
        } else {
            acceptable = true;
        }
    } while (!acceptable);

    return theFilepath;

}
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Yes, but that would also run again all the way trough the thread of FileChooser.showSaveDialog. My idea was somehow to stop FileChooser from closing unless the file does not overwrite anything or the user approves overwriting. Your idea is quite similar to running again the entire function, but done in an iterative way rather than the recursive FileSavePath() if (!acceptable) is true. Thank you for your answer, but I am looking for something more efficient, I am quite desperate for efficience :) – Mihai Bujanca Nov 25 '12 at 23:49
  • As far as I'm aware, `JFileChooser` does not supply any way to intercept the "approval" event. The only way you know that the user has made a choice is when the chooser returns (and the modal dialog begin used by the chooser is closed) – MadProgrammer Nov 25 '12 at 23:54
  • For now, I suppose I will leave it as it is, but I will still need a way to make this more efficient, my app needs this a lot, so far it is way too slow (with many other processes opened, took up to 30 sec for the jfilechooser to **open**) With less processes but eclipse still open, up to 7-10 sec anyway, so I'd have an annoyed user. Also noticed that second time I open the jfilechooser it takes way less time (up to 5 sec with many processes, up to 1.5 sec with less). I am considering the idea of trying a thread that would somewhere take place during the progress of the showConfirmDialog – Mihai Bujanca Nov 26 '12 at 00:06
  • Somethere just before the disposing of the `jfilechooser` – Mihai Bujanca Nov 26 '12 at 00:07
  • 1
    Remember, Swing is **NOT** thread safe. You must only ever interact with the UI components from within the context of the Event Dispatching Thread. In you're case, I would create a factory or utility class that would lazy load the `JFileChooser` and maintain an active reference to it for the duration of the application (or some other event), this will make calling the method faster in the future – MadProgrammer Nov 26 '12 at 00:17
  • Maybe I can make somehow a handler for the save button, so I still might get the name of the file and check for its existence, but the `JFileChooser` should not dispose until the work with it is done. Worth a try, though it's not very likely to work. I am considering maintaining an active reference, since the user will probably save his work at the end of the session, so the reference is not likely to slow down any other functions of the app. Or maintain the reference for a short period of time, considering that the user maybe changed his mind since he does not finally save after 5 minutes :) – Mihai Bujanca Nov 26 '12 at 16:58