Results 1 to 3 of 3
Thread: SaveAs button click
- 07-06-2010, 08:15 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
SaveAs button click
And in my code , 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?'
<html>
<p>
map_saveas.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println(mainFrame.getTitle());
String title=mainFrame.getTitle();
String s5="";
if(title.indexOf("-")!=-1)
{
int Index=title.indexOf("-");
s5=title.substring(0, Index);
}
else
{
s5=mainFrame.getTitle();
}
File wrkDir = new File("c:/nis/Projects/"+s5+"/");
JFileChooser chooser = new JFileChooser();
setFileChooserFont(chooser.getComponents());
chooser.setCurrentDirectory(wrkDir);
chooser.setDialogTitle("Save As");
chooser.setAcceptAllFileFilterUsed(false);
chooser.setFileFilter(new SaveMapfilter());
int returnVal = chooser.showSaveDialog(null);
File fil=chooser.getSelectedFile();
String filename=fil.getName();
String newfilename;
int index=filename.indexOf(".");
System.out.println(index);
String filename1=fil.getName();
//String s=form.getText(0);
String s2=s5+"-"+filename1+".map";
setTitle(s2);
if(index==-1)
{
String s6="c:/nis/Projects/"+s5+"/"+filename;
File f = new File(s6);
try{
if(f.mkdir())
System.out.println("Directory Created");
else
System.out.println("Directory is not created");
}catch(Exception e5){
System.out.println(e5);
}
String sub=filename+".map";
newfilename=sub;
String s8="c:/nis/Projects/"+s5+"/"+filename+"/"+sub;
fil=new File(s8);
}else {
String sub1=filename.substring(0,index);
String sub2=sub1;
String s6="c:/nis/Projects/"+s5+"/"+sub2;
File f = new File(s6);
try{
if(f.mkdir())
System.out.println("Directory Created");
else
System.out.println("Directory is not created");
}catch(Exception e5){
System.out.println(e5);
}
String sub=filename;
newfilename=sub;
String s8="c:/nis/Projects/"+s5+"/"+sub2+"/"+sub;
fil=new File(s8);
}
if (returnVal == JFileChooser.APPROVE_OPTION) {
BufferedWriter bwriter1=null;
try {
//*********************
FileWriter fwriter = new FileWriter(fil);
// FileWriter fwriter = new FileWriter(newfile);
BufferedWriter bwriter = new BufferedWriter(fwriter);
}catch(Exception exception)
{System.out.println(exception);}
// System.out.println(newfilename);
try {
FileWriter fwriter = new FileWriter(fil);
// FileWriter fwriter = new FileWriter(newfile);
BufferedWriter bwriter = new BufferedWriter(fwriter);
for (int i=0;i<mappingTable.getRowCount();i++) {
for (int j=0;j<mappingTable.getColumnCount();j++) {
String s=String.valueOf(mappingTable.getValueAt(i,j));
//System.out.println(s);
int l=s.length();
//System.out.println(l);
if(s.equals("null"))
{
bwriter.write(" ");
s=" ";
//System.out.println(s);
}
// bwriter.write( String.valueOf(table.getValueAt(i,j))+"" );
bwriter.write(s );
bwriter.newLine();
}
//bwriter.newLine();
}
bwriter.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
//****************
//*********************
// BufferedWriter bwriter1=null;
// System.out.println(newfilename);
try {
FileWriter fwriter1 = new FileWriter(file,true);
bwriter1 = new BufferedWriter(fwriter1);
bwriter1.newLine();
bwriter1.write(newfilename );
// bwriter1.newLine();
bwriter1.close();
} catch (IOException ioe){
ioe.printStackTrace();
}
//****************
}
}
});
int saveasactions=map_saveas.getActionListeners().leng th;
if(saveasactions>1)
{
for(int i=0;i<(saveasactions-1);i++)
{
map_saveas.removeActionListener(map_saveas.getActi onListeners()[i]);
}
}
</p>
</html>
- 07-06-2010, 08:26 AM #2
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
Sir,
post quick reply
- 07-06-2010, 09:35 AM #3
Similar Threads
-
How to show new window after click on button
By javahoush in forum SWT / JFaceReplies: 4Last Post: 12-29-2009, 10:25 AM -
How can I display on Button click?
By ntagrafix in forum New To JavaReplies: 3Last Post: 11-04-2009, 01:05 AM -
How we can open a file with a click in button
By kostinio in forum AWT / SwingReplies: 2Last Post: 11-01-2009, 05:46 PM -
deselecting a button after the click.
By ramsrocker in forum New To JavaReplies: 10Last Post: 02-15-2009, 07:52 AM -
AWT - catching click button event
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 12:02 AM
Bookmarks