Tutorial Tutorial Step 08

Step 7: Add a menu event handler to clear the TextArea

Let's hook up the File|New menu item to an event handler that clears the TextArea.

  1. Switch to the Design tab and select the File|New menu item in the Tree (menuItem1).
  2. Select the actionPerformed event in the Inspector, then double-click on its value field.
  3. Edit the event handling method to read as follows:
    void menuItem1_actionPerformed(ActionEvent e) {
      // Handle the File|New menu item.
      textArea1.setText(""); // clears the text of the TextArea
    }
    
  4. Run the application and see what the effect is of operating this menu item after you have typed text into the TextArea. It should erase it. Notice that it doesn't ask you if you want to save your file first.

    To handle that, you'll have to set up infrastructure for reading and writing text files, for tracking whether the file has changed and needs saving, and so on. Let's begin the file support in the next step.

Tutorial Tutorial Step 08