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