views:

187

answers:

2

How would I link the file choosen from a JFileChooser to a file and how would I convert it to string being able to display and edit it in a TextArea?

I have the GUI set up using swing, but the link between actionListener and the JFileChooser is not complete.

Any help would be much appreciated.

Code: http://pastebin.com/p3fb17Wi

EDIT: I found this program, that does pretty much what i wanted to, but it does not allow me to save the actual file : http://www.java-forums.org/new-java/8856-how-get-content-text-file-write-jtextarea.html

+2  A: 

To be able to save the changes you have made, you will have to use a Save Dialog. In the example you have quoted, a File Open Dialog is used. They work in a similar way, all that you need to do is then get the file to which the user would like to store the changes made, open a stream to it and write the data back. This tutorial shows you how to use the various File Choosers.

npinti
+1  A: 

All text components support a read(...) and write(...) method. So all you have to do is get the name of the File and create your FileReader or FileWriter and then invoke the method.

All the file chooser is used for is the get the File name to be used by the reader or writer. So the basic code would be:

File saveFile = chooser.getSelectedFile();
FileWriterr writerr = new FileWriter( saveFile );
textArea.write(writer)

Of course you will probably want to use a Buffered reader/writer.

camickr