GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes JfileChooser selecting existing file.......? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "JfileChooser selecting existing file.......?" Watch "JfileChooser selecting existing file.......?" New topic
Author

JfileChooser selecting existing file.......?

prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120

Hi Ranchers,
Im using jfilechooser to save a file in swing; but when the saveAs dialog is opened, im selecting an already existing file to be saved and

it should ask for do you want to replace the existing file?

an dialog should be displayed, and in that if we choose YES?
it should replace the existing file

and if we choose NO?
the saveAs dialog should be still opened and the user should specify a new
file name

and if the user selects CANCEL?
the saveAs action should be cancelled.

Also if already existing file is selected, for me along with its extension the file name is selected.

For example:
If already existing file name is 'log.txt' and if i select the 'log' file from the saveAs dialog opened, it saves the new file name as 'log.txt.txt'.

But instead an error message should be thrown as the file name already exists.?

Looking forward for ranchers help in this?


Regards,
Prabhu.

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
JFileChooser doesn't do any of this. It serves merely to select a file name. Afterwards it's up to the application to do whatever is appropriate with that name (like ask the user if he wants to overwrite an existing file).

The application could use a JOptionPane for the yes/no question. If the user says no (meaning the file should not be overwritten), then the application would go back to showing the JFileChooser.

It's also possible to add this code right in the JFileChooser dialog, by adding an accessory (see the setAccessory method). This article shows how to add accessories.


Ping & DNS - my free Android networking tools app
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

I have answered a similar question in this thread.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120

Hi ROB,
I saw your post, but in my case i need the save as dialog still enabled if an already existing file is selected to save and it asks for 'File already exist, do you want to replace it?' If i say yes, it can replace it and If i click CANCEL, the save as dialog should be again poped up and we should give another filename.


Here is my code:

CODE 1:

private void btnSaveAsActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory( new File( "./") );
if ( chooser.showSaveDialog( this ) == JFileChooser.APPROVE_OPTION )

{
File fileName = new File( chooser.getSelectedFile( ) + ".log" );
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
}


But i changed it as:

CODE 2:

try
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory( new File( "./") );
int actionDialog = chooser.showSaveDialog(this);
if ( actionDialog == JFileChooser.APPROVE_OPTION )
{
File fileName = new File( chooser.getSelectedFile( ) + ".log" );
if(fileName != null)
{
if(fileName.exists())
{
actionDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
if (actionDialog == JOptionPane.NO_OPTION)
{
chooser.showSaveDialog(this);
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
if(actionDialog == JOptionPane.YES_OPTION)
{
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
return;
//AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFileAlreadyExists"));
}

BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );

}
}
}

And in my code 2, it is working fine, but when i click save as button, save as dialog pops up and if i give an already existing file name, it throws an error dialog as "File already exists, do you want to replace?' and if i click CANCEL, the save as dialog again pops up and if i give the same file name which is already existing, it saves, without giving error dialog, that 'File name already exist?'

I do not know why it happens,

Looking forward for your help in this ROB.

Regards,
Prabhu.

prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120

Also ROB,

If i select a file name from the file directory, from the save as dialog, It selects the file name with its extension also, for example: if the file name is 'log' and its extension is '.txt' and if i select the same file name to save, it takes the file name as 'log.txt' and if i save this, it treats it as different file name, and saves as 'log.txt.txt', please help our in this issue also ROB.

Regards,
Prabhu.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3606
    
  15
Originally posted by prabhu rangan:
Hi ROB,
I saw your post, but in my case i need the save as dialog still enabled if an already existing file is selected to save and it asks for 'File already exist, do you want to replace it?' If i say yes, it can replace it and If i click CANCEL, the save as dialog should be again poped up and we should give another filename.


Did you try what Ulf suggested in the last paragraph of his post ?


Joanne
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120

Hi Neal,

I could not understand what ulf is saying; can you please provide me some solution for my questions.

Regards,
Prabhu.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3606
    
  15
I've never used the JFileChooser.setAccessory method, but looking at the JavaDocs and the Java Swing tutorial it sounds like it might do what you need. Ulf has provided a link with an example in and there is another example in the Java Swing tutorial, so why don't you give it a go.
If you can't get it to do exactly what you want, post your code here and maybe someone will be able to assist, but you need to Show Some Effort first.

You might also get some helpful suggestions if a moderator moved this to the Swing forum.
[ June 12, 2008: Message edited by: Joanne Neal ]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42061
    
  64
Originally posted by prabhu rangan:
can you please provide me some solution for my questions.

That's not how JavaRanch works. We're happy to help you arrive at solutions, but won't provide them for you. So: What have you tried? What didn't you understand about the "accessory" concept? Did you get the sample code in the article to run? When you looked at its code, did you get any ideas how it might help in your case?
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120

Hi Ulf,
I have posted my code above, which I have tried to find the solution for my questions. But the exact solution i can not get. Please go through my post once again and you can see my code there, if i have made any mistakes, please correct me.

Regards,
Prabhu.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Prabhu, please don't update old posts if there have been replies after that post. That will confuse people. Instead just put everything in the new message.

Anyway, you either have to stop the JFileChooser from closing until either cancel is pressed, a non-existing file is selected or the user chose to overwrite the file (see the link in my first post for a way), or loop:
prabhu pandurangan
Ranch Hand

Joined: May 23, 2008
Posts: 120

posted Thursday, June 12, 2008 9:54 AM Profile for prabhu rangan Author's Homepage Email prabhu rangan Send New Private Message Edit/Delete Post Reply With Quote

quote:
Hi ROB,
I saw your post, but in my case i need the save as dialog still enabled if an already existing file is selected to save and it asks for 'File already exist, do you want to replace it?' If i say yes, it can replace it and If i click CANCEL, the save as dialog should be again poped up and we should give another filename.


Here is my code:

CODE 1:

private void btnSaveAsActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory( new File( "./") );
if ( chooser.showSaveDialog( this ) == JFileChooser.APPROVE_OPTION )

{
File fileName = new File( chooser.getSelectedFile( ) + ".log" );
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
}


But i changed it as:

CODE 2:

try
{
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory( new File( "./") );
int actionDialog = chooser.showSaveDialog(this);
if ( actionDialog == JFileChooser.APPROVE_OPTION )
{
File fileName = new File( chooser.getSelectedFile( ) + ".log" );
if(fileName != null)
{
if(fileName.exists())
{
actionDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
if (actionDialog == JOptionPane.NO_OPTION)
{
chooser.showSaveDialog(this);
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
if(actionDialog == JOptionPane.YES_OPTION)
{
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
return;
//AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFileAlreadyExists"));
}

BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );

}
}
}

And in my code 2, it is working fine, but when i click save as button, save as dialog pops up and if i give an already existing file name, it throws an error dialog as "File already exists, do you want to replace?' and if i click CANCEL, the save as dialog again pops up and if i give the same file name which is already existing, it saves, without giving error dialog, that 'File name already exist?'

I do not know why it happens,

Looking forward for your help in this ROB.

NOTE: Everything works fine for the first time, if i give the file name to save which is already exists, it throws an error dialog, and ask for 'File exist do you want to replace?' and if i click YES it replaces it; but if i click NO, the save as dialog opens again and again if i give the same name which is already exist, it is not throwing any error message, it simply replaces it with the new one.

Question: For me error dialog should be thrown for all the time when i give a file name that already exists.?

Please be patient and answer me rob!.... as my post may be simple and being for long time, i could not find the solution....

Regards,
Prabhu.


 
GeeCON Prague 2014
 
subject: JfileChooser selecting existing file.......?