/* FontChooser.java This program displays the list of font typefaces available on the system. (I suppose technically it should be called `TypefaceChooser'). When the user clicks on the name of a typeface, it displays some text in that typeface. This is a quite a useful program to have, as it can assist in the selection of fonts for a program's display. The display looks like this:(Note that you will only see the images if you are looking at the HTML version of this file, FontChooser.java.html). Kevin Boone, August 1999 */ import java.applet.Applet; import java.awt.*; import java.awt.event.*; /* class FontChooser This program has only one class, which is a subclass of Applet as usual. Note that we define the class to `implememts ItemListener'. This specifies it as being able to receive events from the list component. When the user clicks on an item in the list, this program receives an ItemEvent event. This causes the operation itemStateChange to be executed. */ public class FontChooser extends Applet implements ItemListener { /* The display consists of two parts: the list of fonts (fontList) and the font display area (fontDisplayLabel). Since the display shows text, it is implemented as a label. These objects have to be stored as attribtues, as they are created in the constructor but used in the operation itemStateChange. */ Label fontDisplayLabel; List fontList; /* FontChooser constructor */ public FontChooser() { super(); // A BorderLayout is probably the right thing to use here. We will put the main // part of the display (the font list) in the middle, and the text at the top setLayout(new BorderLayout()); // This very ugly line retrieves the list of font typefaces as an array of Strings String listOfFonts[] = GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames(); // Create the font list object, and put the names of the typefaces in it fontList = new List(); for (int i = 0; i < listOfFonts.length; i++) fontList.add(listOfFonts[i]); add(fontList, BorderLayout.CENTER); // We now use addItemListener to specify that `this' (that is, this applet) // will handle events from the list fontList.addItemListener(this); // Now create the font display area. It starts of empty, as the user has not // selected a font yet fontDisplayLabel = new Label(""); add(fontDisplayLabel, BorderLayout.NORTH); } /* itemStateChanged This operation is called whenever the user clicks on a font name (that is, any item in the font list */ public void itemStateChanged (ItemEvent e) { // Only proceed if an item is being selected. This operation is also // called for the item that is being de-selected. We don't want // todo anything special about that if (e.getStateChange() == ItemEvent.SELECTED) { // Get the name of the typeface from the list String fontName = fontList.getSelectedItem(); // Create a font with that name Font font = new Font(fontName, Font.PLAIN, 12); // Set the font display label to the selected // font, and insert the name as its text fontDisplayLabel.setText(fontName); fontDisplayLabel.setFont(font); } } }
©1994-2003 Kevin Boone, all rights reserved