lovely_rosa
asked on
Save File
I write simple editor using Jfram , java GUI , one of the function is to open file , when the user open the file he or she want to edit the file , then he \ she choose save
the problem is here , I want to save the opened file on the same opened file without asking me gain to choose name for the file
thankyou in advanced ,
for save action I reuse this method
the problem is here , I want to save the opened file on the same opened file without asking me gain to choose name for the file
thankyou in advanced ,
for save action I reuse this method
class SaveAction extends AbstractAction {
public SaveAction() {
super("Save", new ImageIcon("icons/save.gif"));
}
// Query user for a filename and attempt to open and write the text
// component's content to the file.
public void actionPerformed(ActionEvent ev) {
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(Main.this) != JFileChooser.APPROVE_OPTION)
return;
File file = chooser.getSelectedFile();
if (file == null)
return;
FileWriter writer = null;
try {
writer = new FileWriter(file);
textComp.write(writer);
} catch (IOException ex) {
JOptionPane.showMessageDialog(Main.this,
"File Not Saved", "ERROR", JOptionPane.ERROR_MESSAGE);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException x) {
}
}
}
}
}
If you want to save to the same file from which you reda you should already tknow the file
so just removve this part
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(Ma in.this) != JFileChooser.APPROVE_OPTIO N)
return;
File file = chooser.getSelectedFile();
if (file == null)
return;
and make sure you pass the File or filename to thios method,
You'll need to have this file closed and then opened again for wtriting
so just removve this part
JFileChooser chooser = new JFileChooser();
if (chooser.showSaveDialog(Ma
return;
File file = chooser.getSelectedFile();
if (file == null)
return;
and make sure you pass the File or filename to thios method,
You'll need to have this file closed and then opened again for wtriting
ASKER
please I need more explain and thanks again for you
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did it thanks a lot
I increase the point for u
I increase the point for u
here. Do you have two button - one to save to the same file, another to "save as" ?