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.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
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.
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.
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.
Joanne
Hi Neal,
I could not understand what ulf is saying; can you please provide me some solution for my questions.
Regards,
Prabhu.
Joanne
Originally posted by prabhu rangan:
can you please provide me some solution for my questions.
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.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
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.