Tutorial Tutorial Step 11

Step 10: Add code to menu items for saving a file

Next let's add the code for hooking up the the File|Save and File|Save As menu items for writing the file back out to disk. To do this, you'll need to add to the TextEditFrame class a String instance variable to hold the name of the file that was opened, and add methods for writing the text back out to that file and to other files.

  1. Click on filer1 () in the Structure Pane. This will take you to the last entry in the list of instance variable declarations (since filer1 was the last declaration made).

  2. Add the following declarations to the end of the list:
    String currFileName = null;  // path plus filename. null means new / untitled
    boolean dirty = false;  // true means modified text
    
  3. Click on the openFile() method in the Structure pane to quickly locate it in the source code. Then place the cursor in that method after the following line that reads the file into the TextArea:
       textArea1.setText(new String(data, 0, chars_read));
    
  4. Insert the following code there:

       // Cache the currently opened filename for use at save time...
       this.currFileName = fileName;
       // ...and mark the edit session as being clean.
       this.dirty = false;
    
  5. Create a saveFile() method that you can call from the File|Save event handler. You can place it just after the openFile() method:
    /**
     * Save current file; handle not yet having a filename; report to statusBar.
     * @return false if save did not occur.
     */
    boolean saveFile()
    {
    
      // Handle the case where we don't have a file name yet.
      if (currFileName == null) {
        return saveAsFile();
      }
    
      try
      {
        // Open a file of the current name.
        File file = new File (currFileName);
    
        // Create an output writer that will write to that file.
        // FileWriter handles international characters encoding conversions.
        FileWriter out = new FileWriter(file);
        String text = textArea1.getText();
        out.write(text);
        out.close();
        this.dirty = false;
        return true;
      }
      catch (IOException e) {
        statusBar.setText("Error saving "+currFileName);
      }
      return false;
    }
    

  6. Create the saveAsFile() method that is called from saveFile() if there is no current filename. It will also be used from the File|Save As menu later. Put the following code right after the saveFile() method:
    /**
     * Save current file, asking user for new destination name.
     * Report to statuBar.
     * @return false means user cancelled the SaveAs
     */
    boolean saveAsFile() {
      // Filer makes use of a java.awt.FileDialog, so its
      // mode property uses the same values as those of FileDialog.
      filer1.setMode(FileDialog.SAVE); // Use the SAVE version of the dialog.
    
      // Make the dialog visible as a modal (default) dialog box.
      filer1.show();
    
      // Upon return, getFile() will be null if user cancelled the dialog.
      if (filer1.getFile() != null) {
        // Non-null file property after return implies user
        // selected a filename to save to.
    
        // Set the current file name to the user's selection,
        // then do a regular saveFile.
        currFileName = filer1.getDirectory()+filer1.getFile();
        return saveFile();
      }
      else {
        return false;
      }
    }
    
  7. Switch back to the Design tab and create an event handling method for the File|Save menu item. Select menuItem3 in the Component Tree, then double-click twice in the actionPerformed event on the Events page in the Inspector. Edit the body of the event handling method so it looks like this:
    void menuItem3_actionPerformed(ActionEvent e) {
      saveFile();
    }
    
  8. Repeat the previous step for the File|Save As menu item (menuItem4), making its action Performed event handling method look like this:
    void menuItem4_actionPerformed(ActionEvent e) {
       saveAsFile();
    }
    
  9. Save your file.

Warning: Be careful if you compile and run the program at this point, because this is now a functioning text editor. Don't damage your source files or other important files while testing it.

Tutorial Tutorial Step 11