• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 216
  • Last Modified:

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  


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) {
          }
        }
      }
    }
  }

Open in new window

0
lovely_rosa
Asked:
lovely_rosa
  • 3
  • 2
1 Solution
 
for_yanCommented:
If you want to save to the same file you doen't need to use filechooser and don't need to instantite it
here. Do you have two button - one to save to the same file, another to "save as" ?
0
 
for_yanCommented:
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(Main.this) != JFileChooser.APPROVE_OPTION)
        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

0
 
lovely_rosaAuthor Commented:
please I need more explain and thanks again for you
0
 
for_yanCommented:

FileChooser and this kind of statement :
  JFileChooser chooser = new JFileChooser();
is necessary only when you want to get from the user name of the file (say new
file which you can use for Save as operation).

So if you have the file already opeend - you should already have fole name or File objecty which points to that
file. You don't need to ask the user for that name again - it should be passes form the part of  your program
which knows it (say from the part which opened it).
FileChooser is not necearry for "Save" operation.

FileChooser is onle necceassry for "Save As" where you ask the nameof the new file.

So respond to sucvh question:

do you want to have two options ofor the user in your program - "Save" and "Save As" or only "Save"?

If you want to have two options - do you have two buttons in your GUI whwrere
user can click one or the other?

If you have only "Save" option then forget about FileChooser for the saving stage - you should not need it
at all.

Please, let me knwo what part of it you do not understand - I'll try toexplauin again.


0
 
lovely_rosaAuthor Commented:
I  did  it  thanks  a lot  

I  increase the point  for u  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now