Tutorial Tutorial Step 06

Step 5: Attach a menu item event to the FontChooser dialog

Let's get the FontChooser dialog to interact with the font of textArea1.

  1. Click on the Source tab and go to the Font menu item (menuItem5) event handling method.

    Tip: To quickly locate this method in the source code, click on the following node in the Structure pane (bottom left of the AppBrowser) :

  2. Change your Font menu item (menuItem5) event handling method to read as shown below.

    To save typing, you can copy and paste the code example below from the Help Viewer to your source code by doing the following:

    void menuItem5_actionPerformed(ActionEvent e) {
      // Handle the "Edit Font" menu item
    
      // Pick up the existing font from the TextArea
      // and put it into the FontChooser before showing
      // the FontChooser, so that we are editing the
      // existing / previous font.
      fontChooser1.setValue(textArea1.getFont());
    
      // Show the FontChooser.
      // Since the FontChooser is modal by default,
      // the program will not return from the call
      // to show until the user dismisses the FontChooser
      // using OK or Cancel.
      fontChooser1.show();
    
      // Now that the user has dismissed the FontChooser,
      // obtain the new Font from the FontChooser's
      // value property.  First test the result property to see if the
      // user pressed OK.
      if (fontChooser1.getResult() == FontChooser.OK) {
    
        // Set the font of textArea1 to the font
        // value that can be obtained from the
        // value property of fontChooser1.  This
        // font value is what the user entered
        // before pressing the OK button
        textArea1.setFont(fontChooser1.getValue());
      }
    }
    
  3. Run the application and type some text in the TextArea.

  4. Use the Edit|Font menu item and change the text's font. In this application, the font for the entire text area (not just the selected text) is changed. Test the application to see that each time you bring up the FontChooser dialog, the dialog is picking up the previously selected font value as a starting value.

Tutorial Tutorial Step 06