Tutorial Tutorial Step 08

Building a Java text editor

Step 7: Adding a menu event handler to clear the text area

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

  1. Switch back to the designer.

  2. Select the File|New menu item in the component tree (jMenuItem1).

  3. Create an actionPerformed() event, and insert the following code into it:
      // Handle the File|New menu item.
      // Clears the text of the text area.
      jTextArea1.setText("");
    
  4. Save and run the application, type something into the text area, then see what happens when you choose File|New. It should erase the contents. Notice that it doesn't ask you if you want to save your file first.

    To handle that, you need 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.

  5. Close the "Text Editor" application.

Tutorial Tutorial Step 08