Tutorial Tutorial Step 16

Building a Java text editor

Step 15: Showing filename and state in the window title bar

In this final step, you will add code that uses the title bar of the application to display the current filename, and to display an asterisk if the file is "dirty".

To do this, create a new method that will update the title bar, then call it from places where the code changes either the current file name or the dirty flag. Name this new method updateCaption().

  1. Click the jMenuFileExit_actionPerformed(ActionEvent e) method in the structure pane. This moves the cursor to that event handling method and highlights it in the editor. Click in the editor to place the cursor just above this method and insert the following updateCaption() method:

    // Update the title bar of the application to show the filename and its dirty state.
    void updateCaption() {
      String caption;
    
      if (currFileName == null) {
         // synthesize the "Untitled" name if no name yet.
         caption = "Untitled";
      }
      else {
        caption = currFileName;
      }
    
      // add a "*" in the caption if the file is dirty.
      if (dirty) {
        caption = "* " + caption;
      }
      caption = "Text Editor - " + caption;
      this.setTitle(caption);
    }
    

  2. Now call updateCaption() from each of the places the dirty flag actually changes or whenever you change the currFileName.

    Specifically, put the call updateCaption(); in the following places:

  3. Run your application and watch the title bar as you perform the following operations:

Congratulations! You now have a functional text editor written in JBuilder!

For additional suggestions on improving this tutorial, send email to jpgpubs@inprise.com.

JBuilder Professional and Enterprise users: Proceed to Step 16 to deploy this application and run it from the command line.

Tutorial Tutorial Step 16