Tutorial Tutorial Step 14

Step 13: Hook up event handling to the TextArea

Now let's hook up event handling to the TextArea so your program will be setting the dirty flag whenever typing occurs.

  1. Select textArea1 in the Component Tree, and select the Events tab in the Inspector.

  2. Use double click to select the value of the textValueChanged event, then double click again to create an event handler for that event in your code.

  3. Enter a line of code to set the dirty flag, as follows:
    void textArea1_textValueChanged(TextEvent e) {
      dirty=true;
    }
    
    This will make sure that any character typed in the text area will force the dirty flag to true.

  4. Add the following three lines to top of the okToAbandon() so that now it will really be testing the dirty flag:
     if (!dirty) {
       return true;
     }
    
    The okToAbandon() method should now look like this:
    boolean okToAbandon() {
      if (!dirty) {
        return true;
      }
      message1.setButtonSet(Message.YES_NO_CANCEL);
      message1.setTitle("Text Edit");
      message1.setMessage("Save changes?");
      message1.show();
      switch (message1.getResult()) {
         case Message.YES:
           // Yes, please save changes.
           return saveFile();
         case Message.NO:
           // No, abandon edits,
           // i.e. return true without saving.
           return true;
         case Message.CANCEL:
         default:
           // Cancel.
           return false;
      }
    }
    
  5. At this point, you should save your work, run the program, and test to see that dirty and clean states of the file work properly:

Tutorial Tutorial Step 14