8 Replies - 862 Views - Last Post: 26 May 2012 - 08:58 PM Rate Topic: -----

#1 nawar youssef  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 12-May 12

how to return the current opened file's name and use it in netbean

Posted 25 May 2012 - 08:54 PM

Hi everybody I am using FileChooser to let the user select a file to open a text file. Then when the user finish working on the text I want to allow him/her to save the text without
entering the name or select it again, so all what they need to do is press save button.
I did not find a way to do that in netbeans since I am new to netbeans and to java too, but I thought I can do this in this way :
1- when the user open the file, I can get the path or the name of the file and store it in a private field in the class.
2- when the user press the save button, I open a file and write the text to it, and save it as the name of the opened file, by using get method.
but this way write nothing to the file and stay empty or has no change.
this is part of my code
public class SheefraTest1View extends FrameView {

    private String currentFile;
    
    public void setCurrentFile(String file){
        currentFile = file;
    }
    
    public String getCurrentFile(){
        return currentFile;
    }

and this is the method when I used the currentFile
private void openFileActionPerformed(java.awt.event.ActionEvent evt) {                                         
           
        int option = JOptionPane.showConfirmDialog(null, "Want to save this file ? ");
        if(option == 0){ 
        // Yes = save and open a new one
             int returnValSave = jFileChooser.showSaveDialog(null);
             //save the file
             if (returnValSave == JFileChooser.APPROVE_OPTION) {
                   File fileSave = jFileChooser.getSelectedFile();
                   saveToFile(fileSave, jTextArea);
                   jTextArea.setText("");
                        
                   //open a file
                   int returnValOpen = jFileChooser.showOpenDialog(null);
                        if (returnValOpen == JFileChooser.APPROVE_OPTION) {
                            File fileOpen = jFileChooser.getSelectedFile();
                            
                            setCurrentFile(fileOpen.getAbsoluteFile().toString()); //used to store the current file
                            readFromFile(fileOpen, jTextArea );
                        }else {
                            //cancel
                        }
                        
                    }else{
                        //cancel = do nothing
                    } 
                    
        }else if(option == 1){ 
           // No == don't save and open new one 
           jTextArea.setText("");
           int returnValOpen = jFileChooser.showOpenDialog(null);
                   if (returnValOpen == JFileChooser.APPROVE_OPTION) {
                       File fileOpen = jFileChooser.getSelectedFile();
                       readFromFile(fileOpen, jTextArea );
                   }else {
                      //cancel
                   }
        }else{ 
             // Canceld do nothing  
         }
    }


and finally this is the saveToFile() method
public void saveToFile(File file, JTextArea txt){

        try{
            FileWriter userFile = new FileWriter(file);
            BufferedWriter outBuff = new BufferedWriter(userFile);
            String str = "string for example";
                outBuff.write(str);
                outBuff.close();
                txt.setText(""); // to empty the textArea after the file saved 
        }catch(Exception e){
            System.out.println("Error: " + e.getMessage());
        }
        
    } 


Please if somebody can find where is the error in here or if there is better way to save a file without selecting it again, thank you so much for your help.

Is This A Good Question/Topic? 0
  • +

Replies To: how to return the current opened file's name and use it in netbean

#2 GunnerInc  Icon User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 858
  • View blog
  • Posts: 2,281
  • Joined: 28-March 11

Re: how to return the current opened file's name and use it in netbean

Posted 25 May 2012 - 08:57 PM

*** Wrong Forum - moved ***
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to return the current opened file's name and use it in netbean

Posted 25 May 2012 - 09:27 PM

If you want to save it with the name it has when opened, why do you call jFileChooser again ?

int returnValSave = jFileChooser.showSaveDialog(null);
Was This Post Helpful? 0
  • +
  • -

#4 nawar youssef  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 12-May 12

Re: how to return the current opened file's name and use it in netbean

Posted 25 May 2012 - 11:29 PM

View Postpbl, on 25 May 2012 - 09:27 PM, said:

If you want to save it with the name it has when opened, why do you call jFileChooser again ?

int returnValSave = jFileChooser.showSaveDialog(null);


Yeh, that what I want exactly, I want to save it with the name it has when opened, but I searched for the code for that and did not find it.
Do you mean that all what I have to do is
int returnValSave = jFileChooser.showSaveDialog(null);
and it will be saved with its name? if that is, it will be great. I will try it tomorrow and let you know
thank you so much
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to return the current opened file's name and use it in netbean

Posted 26 May 2012 - 10:59 AM

Obviously you've got a File object from a first call to FileChooser to open the file the first time. So just save this File object somewhere and use it to close your file later on without calling FileChooser again
Was This Post Helpful? 1
  • +
  • -

#6 nawar youssef  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 12-May 12

Re: how to return the current opened file's name and use it in netbean

Posted 26 May 2012 - 02:45 PM

View Postpbl, on 26 May 2012 - 10:59 AM, said:

Obviously you've got a File object from a first call to FileChooser to open the file the first time. So just save this File object somewhere and use it to close your file later on without calling FileChooser again

- I am sorry I did not understand what you write yesterday I was so sleepy,
- I used two jFileChoosers, - to save the old file that appear in textArea before - open a new file.
- What I mean in my first question is : if there a way to save the current file to its old name without using jFileChooser again, just clicking the save button?
- To do so, I am trying to store the file name when it opened, and put it in a variable like this
private String currentFile;

and then use those methods to set and get it
public void setCurrentFile(String file){
        currentFile = file;
}
public String getCurrentFile(){
        return currentFile;
}

then when the user press the save button the code run
File crntFile = new File(getCurrentFile() + ".txt");
        saveToFile(crntFile, jTextArea);  

and then inside the saveToFile() method I open a new file with the stored name and write to it so I can save it without needing to jFileChooser, but it did not work I don't know why.
I am sorry I know that my idea and my code is a little missy, because I am new to java, and couldn't find the right code, so I am trying to do it in my own way.
public void saveToFile(File file, JTextArea txt){
        Code codedText = new Code(txt.toString());
        codedText.codeText();
        int num;
        try{
            FileWriter userFile = new FileWriter(file);
            BufferedWriter outBuff = new BufferedWriter(userFile);
            String strCoded = new String(codedText.getCodedText());
                outBuff.write(strCoded);
                outBuff.close();
                //txt.setText("");
        }catch(Exception e){
            System.out.println("Error: " + e.getMessage());
        }
        
    } 

Thank you again ;)
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to return the current opened file's name and use it in netbean

Posted 26 May 2012 - 04:05 PM

Read, and try to understand, my post #5
Was This Post Helpful? 1
  • +
  • -

#8 nawar youssef  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 19
  • Joined: 12-May 12

Re: how to return the current opened file's name and use it in netbean

Posted 26 May 2012 - 08:04 PM

View Postpbl, on 26 May 2012 - 04:05 PM, said:

Read, and try to understand, my post #5

Thank you, it works.
I am new to English, so I have some problem in understand everything quickly, and new to Java and most of my mistakes are logic mistaks.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: how to return the current opened file's name and use it in netbean

Posted 26 May 2012 - 08:58 PM

View Postnawar youssef, on 26 May 2012 - 11:04 PM, said:

and new to Java and most of my mistakes are logic mistaks.

Once your code compiles this is true for any language
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1