Tutorial Tutorial Step 13

Building a Java text editor

Step 12: Activating the toolbar buttons

Since you checked the Generate Toolbar option in the Application wizard, JBuilder generated code for a JToolBar and populated it with three JButton components that already display icons. All you have left to do is specify the text for each button's label and tool tip, and create an actionPerformed() event for each button from which you'll call the appropriate event-handling method.

Specifying button tool tip text

To specify tool tips for the buttons,

  1. Switch back to the UI designer.
  2. Select jButton1 in the component tree, then click the Properties tab in the Inspector.
  3. Click the toolTipText property to highlight its entry. Type Open File, if it doesn't already say that, and press Enter.
  4. Repeat this process for jButton2 and jButton3, using the following text:

Creating the button events

Up until now, you have created event handlers through the Inspector. Let's use a shortcut to create the button events that's much faster.

Many controls define a "default" event in their BeanInfo class. For example, a button defines actionPerformed() as its default event. To quickly generate an event handler for the default event, double-click the control in the UI designer.

Using this shortcut, create events for the buttons as follows:

  1. Double-click jButton1 in the UI designer. This should switch you to the editor and place your cursor inside the new jButton1_actionPerformed(ActionEvent e) event for the Open button.

  2. Enter the following code to call the fileOpen() method:

    //Handle toolbar Open button
    fileOpen();
    

  3. Create a jButton2_actionPerformed(ActionEvent e) event for jButton2 and call saveFile() from it:

    //Handle toolbar Save button
    saveFile();
    

  4. Create a jButton3_actionPerformed(ActionEvent e) event for jButton3, and call helpAbout() from it:

    //Handle toolbar About button
      helpAbout();
    

Notice that the code in the jButton1 and jButton3 event-handlers make calls to methods which don't exist yet: fileOpen() and helpAbout(). Let's create those methods now.

Creating a fileOpen() method

The purpose of the fileOpen() method will be to perform the operations that are currently in your File|Open menu handling method. However, since you need to perform the same operations when the Open button is pressed, you'll create the fileOpen() method so you can have just one copy of that code, and call it from both the File|Open menu and the Open button.

You can use the following steps to create the method:

  1. Create a fileOpen() method stub. You can put this method just above the openFile() method. The stub should look like this:

    // Handle the File|Open menu or button, invoking okToAbandon and openFile
    // as needed.
     void fileOpen() {
    }
    

  2. Select all the code (except the first comment line) inside your existing File|Open event handler, jMenuItem2_actionPerformed(). The code selected should be:
    if (!okToAbandon()) {
      return;
    }
    // Use the OPEN version of the dialog, test return for Approve/Cancel
    if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
      // Call openFile to attempt to load the text from file into TextArea
      openFile(jFileChooser1.getSelectedFile().getPath());
    }
    this.repaint();
    
    

  3. Cut this code from the source code to the clipboard, and paste it into the new fileOpen() method stub.

    Tip: Quickly search in the editor using Search|Find.

    Here is what the completed fileOpen() method should look like:

    // Handle the File|Open menu or button, invoking okToAbandon and openFile
    // as needed.
    void fileOpen() {
      if (!okToAbandon()) {
        return;
      }
      // Use the OPEN version of the dialog, test return for Approve/Cancel
      if (JFileChooser.APPROVE_OPTION == jFileChooser1.showOpenDialog(this)) {
        // Call openFile to attempt to load the text from file into TextArea
        openFile(jFileChooser1.getSelectedFile().getPath());
      }
      this.repaint();
    }
    
  4. Now, call fileOpen() from the File|Open event hander, which you should modify to look like this:

    void jMenuItem2_actionPerformed(ActionEvent e) {
      // Handle the File|Open menu item.
      fileOpen();
    }
    

Creating a helpAbout() method

Now do a similar thing for the Help|About menu item and the About button. Gather the code that is currently in the Help|About event handler into a new helpAbout() method and call it from both the menu and button event handlers.

Use the following steps to do this:

  1. Place the following stub in your code for a new helpAbout() method just before the fileOpen() method:
    // Display the About box.
    void helpAbout() {
    }
    

  2. Cut the following code from jMenuHelpAbout_actionPerformed() into the new helpAbout() method stub:

    TextEditFrame_AboutBox dlg = new TextEditFrame_AboutBox(this);
    Dimension dlgSize = dlg.getPreferredSize();
    Dimension frmSize = getSize();
    Point loc = getLocation();
    dlg.setLocation((frmSize.width - dlgSize.width) / 2 + loc.x, (frmSize.height - dlgSize.height) / 2 + loc.y);
    dlg.setModal(true);
    dlg.show();
    
  3. Insert the call helpAbout(); into jMenuHelpAbout_actionPerformed() so the method looks like this:
    //Help | About action performed
    public void jMenuHelpAbout_actionPerformed(ActionEvent e) {
      helpAbout();
    }
    
  4. Now, save and run the application. Try the Open, Save, and About buttons. Compare them with the File|Open, File|Save, and Help|About menu items.

  5. Close the "Text Editor" application.

Tutorial Tutorial Step 13