Saving a File using JFileChooser

Hi all,

In my application I want the user to be able to save a log of a chat session with another user and save it where they wish. I'm using the JFileChooser and nothing is being saved and I'm not sure where I have gone wrong.

The following is a snippet of the code:-

elseif (event.getSource() == saveLog){

JFileChooser fc =new JFileChooser();

int returnVal = fc.showSaveDialog(this);

//name the file

File outputFile =new File("ChatLog.txt");

//write the contents of the JEditorPane to the file

try{

BufferedWriter out =new BufferedWriter(new FileWriter(outputFile));

out.write(recv.getText());

out.close();

}

catch (IOException e){

}

//think the problem is here

if(returnVal == JFileChooser.APPROVE_OPTION){

fc.setSelectedFile(outputFile);

}

//System.out.println(recv.getText());

}

So when I create the File it automatically saves elsewhere which I don't want. Instead I want the user to specify where it should be saved.

Cheers,

Chris

[1822 byte] By [lordflashearta] at [2007-10-2 10:58:34]
# 1
HINTOnce the user has selected the file they want in the chooser do you think you want to GET or SET the selected file?
lordflashearta at 2007-7-13 3:25:53 > top of Java-index,Java Essentials,New To Java...
# 2

Maybe I'm missing something here so sorry if I'm being a prat. The file the user "gets" is already created (i.e. outputFile). So it is that file I wish to save. I was kinda hoping that when the JFileChooser opens that file is shown in the file name area by default and then the user can open up their directories to locate where they wish to save the file.

Having played around with the File Chooser it appears that to get a file in the file name box the user must select the file from their own PC which seems to me to defeat the purpose of wanting to save a file that has been sent to them. I guess the hint is in the name "Chooser" so is there a way I can get it to do what I wish?

Again apologies if you think I'm a nutter here ;-)

lordflashearta at 2007-7-13 3:25:53 > top of Java-index,Java Essentials,New To Java...
# 3

I don't think you're a nutter but I don't really get what you are asking.

Okay here is is.

Whether the user is being prompted to save a file or open a file once they have selected the file (to save or open) you get that file with getSelectedFile.

If you want to select a certain file by default for the user as you are showing the the dialog then set is what you want.

I am not sure what you are trying to do. You have a file and then... you want the user to choose a new location for the file? If that is what you are asking then you will use get after the dialog not set.

I guess to boil it down to it's simplest terms after the user has dismissed the dialog (and when you are looking at the approve option the user has closed the dialog) it does not make any sense to use set for any reason. Only get.

I hope this helps.

lordflashearta at 2007-7-13 3:25:53 > top of Java-index,Java Essentials,New To Java...
# 4

Hey,

Basically the app is an Instant Messenger and I want the user to have the option of saving their chat session. When the user clicks "Save Log" the contents of the window are saved into a file by the app and then I want the user to specify the location of where they wish to save the file.

I understand by default that the file is already saved in the same location as where the program is running which for my purposes is not really an option.

By using the setSelectedFile option (passing the created file as the argument)I thought that it would set the file created by the app into the JFileChooser in the File Name field. Instead no name appears in the File Name field and that is what I need to achieve.

I've tried changing set to get and move it out of the if APPROVE OPTION but it still don't work.

In essence I want:-

1. The user selects "Save Log"

2. The contents of the chat window are saved into a file which is created by the application

3. The user now selects where they wish to save that file

I hope that is a bit clearer.

Cheers,

Chris

lordflashearta at 2007-7-13 3:25:54 > top of Java-index,Java Essentials,New To Java...
# 5

Here's a fragment of your code:int returnVal = fc.showSaveDialog(this);

//name the file

File outputFile = new File("ChatLog.txt");

What you do there is this:

1. Display the JFileChooser.

2. When the user clicks on any button, either Save or Cancel, that decision is returned to you as the variable returnVal.

3. You ignore this decision and proceed to try saving the file, even if the user pressed Cancel.

4. You don't bother to find out what file the user selected, you proceed to save the file in some predetermined location.

Here's an improved version of that fragment:int returnVal = fc.showSaveDialog(this);

if (returnVal == JFileChooser.APPROVE_OPTION) {

//name the file

File outputFile = fc.getSelectedFile();

You could also try reading the tutorial. There's a link to it in the API documentation.

DrClapa at 2007-7-13 3:25:54 > top of Java-index,Java Essentials,New To Java...
# 6

The code looks a bit randomized. Here it is in proper order, with a call to getSelectedFile() added:

} else if (event.getSource() == saveLog) {

// name the file

File outputFile = new File("ChatLog.txt");

JFileChooser fc = new JFileChooser();

fc.setSelectedFile(outputFile);

int returnVal = fc.showSaveDialog(this);

if (returnVal == JFileChooser.APPROVE_OPTION) {

outputFile = fc.getSelectedFile();

// write the contents of the JEditorPane to the file

try {

BufferedWriter out = new BufferedWriter(new FileWriter(outputFile));

out.write(recv.getText());

out.close();

} catch (IOException e) {

// handle error here

}

}

}

leifsa at 2007-7-13 3:25:54 > top of Java-index,Java Essentials,New To Java...
# 7

Hi,

I am having the same problem, when saving a file it saves in the working directory of the program, not the place specified by the JFileChooser, has anyone found the problem?

Some of my code:

else if(evt.getSource() == saveAsImage)

{

try

{

File file = new File("default.jpg");

JFileChooser chooser = new JFileChooser();

chooser.setSelectedFile(file);

int returnValue = chooser.showSaveDialog(this);

if(returnValue == JFileChooser.APPROVE_OPTION)

{

file = chooser.getSelectedFile();

scrnImg.createImage(compsPainter, file.getName());//create jpg

JOptionPane.showMessageDialog(null, "JPG Image created successfully",

"Image saved", JOptionPane.INFORMATION_MESSAGE);

}

What am I doing wrong?

Thanks,

Kelly

Kelly_1984a at 2007-7-13 3:25:54 > top of Java-index,Java Essentials,New To Java...
# 8
Hi Kelly,The call to file.getName() extracts the file name part from the path, and I assume the createImage() method then uses that relative to the current directory. Try using file.getPath() instead.
leifsa at 2007-7-13 3:25:54 > top of Java-index,Java Essentials,New To Java...
# 9
Many thanks, works perfect!Kelly
Kelly_1984a at 2007-7-13 3:25:54 > top of Java-index,Java Essentials,New To Java...