Step 4


Overview

In this step you will add the capability of saving text from the text area to a file. The modifications are similar to Step 3, but easier. In the FileViewPane class, you will add a writeText() method. It has a File parameter to specify the destination file.

In the FileViewer class, you will add a saveFile()() method. This method uses your JFileChooser dialog to get a file selected by the user, then sends a writeText() message to the file view pane.

In the FileViewer class, you will also create an action object for saving a file. This object will be used to create a "Save As ..." menu item in the "File" menu. Its actionPerformed() method just invokes the saveFile() method.

The code that you will add in this step is used as follows. When a user selects the "Save As ..." menu item, a dialog for selecting a file pops up. After the user selects a file, it is used as the parameter in a saveFile() message. In saveFile(), the file is forwarded to the file view pane in a writeText() message. The writeText() method saves the contents of the text area into the selected file.

Writing Text from a FileViewPane

The "Save As..." action is supported in the FileViewPane class with the writeText() method. This method has void return type and a parameter of type File. It should be declared public since it is invoked from a different class.

The code for the writeFile() method is all in a try-catch statement like the one in Step 3. In fact, the catch clause is identical, and the try clause is easier. For the try clause, you should first declare a FileWriter local variable. It should be initialized by invoking the FileWriter constructor, with the writeText() parameter forwarded as the constructor parameter. Then send a write() message to the file writer, passing text from the text area (textArea.getText() as a parameter. Finally, close the file writer with a close() message.

When you have completed the changes, recompile the FileViewPane class to check for syntax errors. The remaining changes are all in the FileViewer class.

An Action Object for Saving a File

The last thing that you need to do in the FileViewer class is to create an action for saving a file and add it to the "File" menu. This is similar to the "Open..." action in Step 3. The name of the action variable should be saveAsAction. Its actionPerformed() method should invoke saveFile(). The name given to the action should be "Save As...".

When you have made the above changes, recompile the FileViewer class and run the program. After it starts, enter some text into the text area. When you open the "File" menu, you should see the new "Save As..." menu item.

Click on it and enter a new file name in the text field at the bottom of the dialog that pops up. Take care not to use the name of one of your .java files - you do not want to overwrite them. Then click on the "Save" button. The program should save the contents of the text area into the file that you named. You can check this by opening the file with the "Open..." menu item. The text in the text area should not change. If it doesn't, you are ready for Step 5.