save a file
Hi,
I have a method that creates a file, and I want to save this file with JFileChooser.
I can't find out how to put automaticly the file name (test.txt) in the textfield"File name" of the savedialog window.
Hereby some code to make things more clear[
code]
FileChooser sa = new FileChooser;
File tf;
public File tekst(File fl){
tf = new File("testfile.txt");
System.out.println("hello");
return tf;
}
public void save_as() {
sa.showSaveDialog(null);
tf = sa.getSelectedFile();
}
[/code]
Thanks for any help, (a am a newbie)
[635 byte] By [
kiekeboea] at [2007-11-27 5:05:52]

setSelectedFile()It takes a File argument. So in your case you would use tf.
I doesn't work?
1. No showing of the file title in the textfield
2. no saving
My code is now( I tried some variations) :
FileChooser sa = new FileChooser;
File tf;
public File tekst(File fl){
tf = new File("testfile.txt");
System.out.println("hello");
return tf;
}
public void save_as() {
sa.showSaveDialog(null);
tf = sa.getSelectedFile();
sa.setSelectedFile(tf);
}
I guess it to be a minor problem.
You seem to be having problems using JFileChooser correctly,
but apart from that, realize that JFileChooser does only one thing: it
let's you choose a file. It doesn't not create files in your file system. It does not
write data to files or read data from files. Again, all it does is allow you to select
a file.
Now what is it that you are are really trying to do? What is your goal?
You can also save files with the SaveFileDialog (see the code)This is a part of a bigger SWING GUI code.The goal is to create a textfile with the same results as the GUI, and let the user save this.(next problem will be: let the user print this file)thanks
correction:you activate the save dialog windows with showSaveDialog()
It's still unclear to mean what your goal is -- use JFileChooser, or save data to a given file. Note that these two goals are completely unrelated.
The goal is to save the file, created by the method :public File tekst(File fl)on the users system.Or to put it in a different way,how to save a textfile, created by a method, save on the users system.
To create a new file, you can use the file method [url=http://java.sun.com/javase/6/docs/api/java/io/File.html#createNewFile()]createNewFile[/url].
If your question is really about saving data to a file, take the IO tutorial: http://java.sun.com/docs/books/tutorial/essential/io/index.html
the question is not how to save data to a file, but to save a file to a OS.
> but to save a file to a OS.I'm confused by that statement. One doesn't "save files". Do you mean create a new, empty file? I gave a link to that. Perhaps you can explain it more. How would you "save a file" by hand?
When one create a (text)file, let's say with notbook, words, kile or whatever, you can save this file by pushing a button, normaly with the name "save as" Then there opens a dialog window, where you can give in the file name and the location where you want to save the file.
OK?
I am creating a file with a method.
the dialog window to save files in java is activated with:
showSaveDialog()
how can I make the link between my file, created by the method and this dialog window, so that in the textfield"save as" already the name appears of my file?
Didn't someone already suggest setSelectedFile?
Quick demo:
import java.io.*;
import javax.swing.*;
public class FileChooserExample {
public static void main(String[] args) {
File file = new File("example.txt");
JFileChooser chooser = new JFileChooser();
chooser.setSelectedFile(file);
int reply = chooser.showSaveDialog(null);
}
}
> showSaveDialog()
Which has nothing to do with putting bytes onto disk.
>
> how can I make the link between my file, created by
> the method and this dialog window, so that in the
> textfield"save as" already the name appears of my
> file?
You have to write the bytes out to the file, with, for example, a FileOutputStream or FileWriter, depending on the semantics of those bytes.
http://java.sun.com/docs/books/tutorial/essential/io/index.html
jverda at 2007-7-21 21:18:43 >

OK, that's what I was looking for.I did not use thesetSelectedFilein the correct way.Thanks for your patience...
I still don't get I right.
hereby some 'code' , I hope this is the best way to explain my problem//calling the method
public void actionPerformed(ActionEvent e) {
if (e.getSource() == savebutton) {
saveas(/*code?*/);
}
//creating the textfile
File tf;
public File tekst(File fl){
tf = new File("tekstdemo.txt");
System.out.println("hello");
return tf;
}
//the method to open the savedialog window, with the name of the file (textdemo.txt) in the 'sava as' textfield?
public void saveas(String [] args) {
JFileChooser ch = new JFileChooser();
ch.setSelectedFile(tf);
int reply = sa.showSaveDialog(null);
}
> I still don't get I right.
>
> hereby some 'code' , I hope this is the best way to
> explain my problem
The code is only part of the picture. You have to explain what problems you're having. Does it not compile? Does it give an error at runtime? Does it run but not do what you want? What's the complete, exact error message? In what way is it not behaving how you want it to?
jverda at 2007-7-21 21:18:43 >
