|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

06-17-2008, 08:04 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
[SOLVED] jfilechooser for saving a file....?
Hi Members,
Im using jfilechooser to save a file, when i click a Save As button, a save as dialog pops up and if i give a file name that is already existing in my directory; it throws an error dialog, 'File already exists, do you want to replace it?' and if i click YES the file is replaced, but when i click NO again the save as dialog should be poped up and the validation should be continued for number of times the functuion is called. but for me only for the first time the valaidation is done and for the second time, if i give the same file name that already exist, it is not throwing any error dialog and simply it saves.
Please help me out in this.
here is my code below:
Here is my code:
CODE 1:
private void btnSaveAsActionPerformed(java.awt.event.ActionEven t 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("LogFil eAlreadyExists"));
}
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 provide me some sample code for my requirement......?
Regards,
Prabhu.
Last edited by prabhurangan : 06-17-2008 at 08:05 AM.
Reason: Added some more details
|
|

06-17-2008, 08:37 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
|
You don't have validate the file is exist or not even after the no options is selected. Just think about the logic there.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-17-2008, 08:42 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,124
|
|
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)
return;
if(fileName.exists())
{
actionDialog = JOptionPane.showConfirmDialog(this,
"Replace existing file?");
// may need to check for cancel option as well
if (actionDialog == JOptionPane.NO_OPTION)
return;
}
// okay to write file
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
//AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString(
"LogFil eAlreadyExists"));
}
|
|

06-17-2008, 08:51 AM
|
 |
Moderator
|
|
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 2,959
|
|
|
Now is the logic you have miss.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Has someone helped you? Then you can To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. their helpful post.
Want to make your IDE the best? To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts. (Close on September 4, 2008)
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
|
|

06-17-2008, 08:55 AM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
Hi Member,
This was the one which im doing, but my project manager says that he needs the save as dialog again, once the user clicks NO option for showConfirmDialog, is it possible to do so? The saveAs dialogshould be always shown when the NO option is clicked for confirmDialog?
Regards,
Prabhu.
|
|

06-17-2008, 12:10 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
|
Hi Eranga,
Im not getting of what you ask for. But this is the logic im in need of. each time when a save as dialog is opened, it should check for File exists, and if NO option is clicked again the dialog should appear and when save button is clicked it should check for File already exists? dialog.
Looking forward for your reply.
Regards,
Prabhu.
|
|

06-17-2008, 01:06 PM
|
|
Member
|
|
Join Date: May 2008
Posts: 49
|
|
Hi Eranga,
Please i need your help in this, issue too.
I could not fix this. Below is my code for your look up.
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" );
File fileName = chooser.getSelectedFile();
String temp = fileName.toString();
String name = temp.substring(temp.lastIndexOf("\\") + 1, temp.lastIndexOf("."));
//File file_name = new File(name);
//System.out.println(name);
if(fileName.exists())
{
int confirmDialog = JOptionPane.showConfirmDialog(this, "Replace existing file?");
if (confirmDialog == JOptionPane.NO_OPTION)
{
chooser.showSaveDialog(this);
}
if(actionDialog == JOptionPane.YES_OPTION)
{
BufferedWriter outFile = new BufferedWriter( new FileWriter( name ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
//AttestDialog.getInstance( ).showErrorDialog(languageBundle.getString("LogFil eAlreadyExists"));
}
else
{
BufferedWriter outFile = new BufferedWriter( new FileWriter( fileName ) );
outFile.write( getTxtArea_log().getText( ) ); //put in textfile
outFile.flush( ); // redundant, done by close()
outFile.close( );
}
}
}
Looking forward for your reply. We should use our customized code to meet my requirement i think so. I should regain the save dialog if cancel button is clicked in the confirm dialog.
Regards,
Prabhu.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|