Activity 7.1 - Constructing an interface for font selection

Topic

In this activity we complete the coding of the class FontDemo, the User Interface of which was developed in the activities for Unit 6. The interface is shown below.

The figure displays a rectangle which has a large area to display text, and various buttons and labelled controls to manage the way the text is displayed. 
The controls are:
 a drop-down list for changing fonts, a text area to change the font size, a tick box to embolden and a tick box for italics.

Materials

We have provided the classes:

FontDemo is functionally identical to the class of the same name in the Unit 6 Activities but the code has been extended. That is, there are a few extra lines of code, some lines are taken out, and all the helper methods have been added.

Task

You are asked to complete the coding of FontDemo so that

But don't worry, you don't have to know all about Java fonts and dialog boxes! We have provided two helper methods to handle the actual mechanics.

When the method updateFontSample is called, it will find out what font is wanted, and set the text sample accordingly. The method aboutDialog will display the required dialog.

Your job is to write the code that will cause the appropriate helper method to be invoked when an event occurs in the User Interface.

You will first need to write an inner class that implements ActionListener. You can name this class anything you please; we used FontDemoListener. It must of course have an actionPerformed(ActionEvent e) method, because otherwise it wouldn't implement ActionListener.

The actionPerformed(ActionEvent e) will have to find what the source of the event was (displayButton or aboutItem) and call updateFontSample() or aboutDialog as appropriate.

The second step will be to add listeners to the button and to the menu item.

(In practice you might write the code the other way around - begin by choosing a name for the listener class and adding the listeners where they are needed, then write the code for the listener class afterwards.)

Instructions

  1. Compile and run the project, setting ex7_1.FontDemoTest as the main class. You should find that the user interface appears but nothing actually happens when the "Show font" button is clicked, or "About" is selected from the menu.
  2. Add an inner class, and action listeners, as described above. You will also need to import java.awt.event.*. We have indicated the places where the extra code must be inserted by comments labelled (A) to (D).
  3. Compile and run the amended code and check that the program is now fully functional.
  4. For our sample solution see the Solution file.

Note

From the previous coverage of packages, you might have expected java.awt.* to have included java.awt.event.*. However, java.awt.event.* is not included in the java.awt package.

All events inherit from the java.util.EventObject class and so have to be specifically imported into any program that needs to handle events.

This approach by the Java language may seem strange but it actually confers a great deal of flexibility - for example allowing you to define events that do not relate conceptually to GUI components.