?
Solved

Save File

Posted on 2011-10-21
5
Medium Priority
?
215 Views
Last Modified: 2012-05-12
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
Comment
Question by:lovely_rosa
  • 3
  • 2
5 Comments
 
LVL 47

Expert Comment

by:for_yan
ID: 37008338
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
 
LVL 47

Expert Comment

by:for_yan
ID: 37008357
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
 

Author Comment

by:lovely_rosa
ID: 37008703
please I need more explain and thanks again for you
0
 
LVL 47

Accepted Solution

by:
for_yan earned 1200 total points
ID: 37008755

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
 

Author Comment

by:lovely_rosa
ID: 37008805
I  did  it  thanks  a lot  

I  increase the point  for u  
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 7 hours left to enroll

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question