Program: Custom Font Chooser

Problem

You want to allow the user to select a font, but standard Java doesn’t yet include a Font Chooser dialog.

Solution

Use my FontChooser dialog class.

Discussion

As we saw in Section 12.4, you can manually select a font by calling the java.awt.Font class constructor, passing in the name of the font, the type you want (plain, bold, italic, or bold+italic), and the point size:

Font f = new Font("Helvetica", Font.BOLD, 14);
setfont(f);

But this is not very flexible for interactive applications. You normally want the user to be able to choose fonts with the same ease as using a File Chooser dialog. Until the Java API catches up with this, you are more than welcome to use the Font Chooser that I wrote when faced with a similar need.

The source code is shown in Example 13-7; it ends, as many of my classes do, with a short main method that is both a test case and an example of using the class in action. The display is shown in Figure 13-13.

Example 13-7. FontChooser.java

import com.darwinsys.util.*;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/** A font selection dialog. AWT version.
 * <p>Note: can take a LONG time to start up on systems
 * with (literally) hundreds of fonts.
 */
public class FontChooser extends Dialog {
    /** The font the user has chosen */
    protected Font resultFont;
    /** The resulting font name */
    protected String resultName;
    /** The resulting font size */
    protected int resultSize;
    /** The resulting boldness */
    protected boolean isBold;
    /** The resulting italicness */
    protected boolean isItalic;

    /** The list of Fonts */
    protected String fontList[];
    /** The file name chooser */
    protected List fNameChoice;
    /** The file size chooser */
    protected List fSizeChoice;
    /** The bold and italic choosers */
    Checkbox bold, italic;
    /** The list of font sizes */
    protected String fontSizes[] = {
        "8", "10", "11", "12", "14", "16", "18", "20", "24",
        "30", "36", "40", "48", "60", "72"
        };
    /** The display area. Use a JLabel as the AWT label doesn't always
     * honor setFont(  ) in a timely fashion :-)
     */
    protected JLabel previewArea;

    /** Construct a FontChooser -- Sets title and gets 
     * array of fonts on the system. Builds a GUI to let
     * the user choose one font at one size.
     */
    public FontChooser(Frame f) {
        super(f, "Font Chooser", true);

        Container cp = this;    // or getContentPane(  ) in Swing

        Panel top = new Panel(  );
        top.setLayout(new FlowLayout(  ));

        fNameChoice = new List(8);
        top.add(fNameChoice);

        Toolkit toolkit = Toolkit.getDefaultToolkit(  );
        // For JDK 1.1: returns about 10 names (Serif, SansSerif, etc.)
        // fontList = toolkit.getFontList(  );
        // For JDK 1.2: a much longer list; most of the names that come
        // with your OS (e.g., Arial), plus the Sun/Java ones (Lucida, 
        // Lucida Bright, Lucida Sans...)
        fontList = GraphicsEnvironment.getLocalGraphicsEnvironment(  ).
            getAvailableFontFamilyNames(  );

        for (int i=0; i<fontList.length; i++)
            fNameChoice.add(fontList[i]);
        fNameChoice.select(0);

        fSizeChoice = new List(8);
        top.add(fSizeChoice);

        for (int i=0; i<fontSizes.length; i++)
            fSizeChoice.add(fontSizes[i]);
        fSizeChoice.select(5);

        cp.add(BorderLayout.NORTH, top);

        Panel attrs = new Panel(  );
        top.add(attrs);
        attrs.setLayout(new GridLayout(0,1));
        attrs.add(bold  =new Checkbox("Bold", false));
        attrs.add(italic=new Checkbox("Italic", false));

        previewArea = new JLabel("Qwerty Yuiop", JLabel.CENTER);
        previewArea.setSize(200, 50);
        cp.add(BorderLayout.CENTER, previewArea);

        Panel bot = new Panel(  );

        Button okButton = new Button("Apply");
        bot.add(okButton);
        okButton.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent e) {
                previewFont(  );
                dispose(  );
                setVisible(false);
            }
        });

        Button pvButton = new Button("Preview");
        bot.add(pvButton);
        pvButton.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent e) {
                previewFont(  );
            }
        });

        Button canButton = new Button("Cancel");
        bot.add(canButton);
        canButton.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent e) {
                // Set all values to null. Better: restore previous.
                resultFont = null;
                resultName = null;
                resultSize = 0;
                isBold = false;
                isItalic = false;

                dispose(  );
                setVisible(false);
            }
        });

        cp.add(BorderLayout.SOUTH, bot);

        previewFont(  ); // ensure view is up to date!

        pack(  );
        setLocation(100, 100);
    }

    /** Called from the action handlers to get the font info,
     * build a font, and set it.
     */
    protected void previewFont(  ) {
        resultName = fNameChoice.getSelectedItem(  );
        String resultSizeName = fSizeChoice.getSelectedItem(  );
        int resultSize = Integer.parseInt(resultSizeName);
        isBold = bold.getState(  );
        isItalic = italic.getState(  );
        int attrs = Font.PLAIN;
        if (isBold) attrs = Font.BOLD;
        if (isItalic) attrs |= Font.ITALIC;
        resultFont = new Font(resultName, attrs, resultSize);
        // System.out.println("resultName = " + resultName + "; " +
        //         "resultFont = " + resultFont);
        previewArea.setFont(resultFont);
        pack(  );                // ensure Dialog is big enough.
    }

    /** Retrieve the selected font name. */
    public String getSelectedName(  ) {
        return resultName;
    }
    /** Retrieve the selected size */
    public int getSelectedSize(  ) {
        return resultSize;
    }

    /** Retrieve the selected font, or null */
    public Font getSelectedFont(  ) {
        return resultFont;
    }

    /** Simple main program to start it running */
    public static void main(String[] args) {
        final JFrame f = new JFrame("Dummy");
        final FontChooser fc = new FontChooser(f);
        final Container cp = f.getContentPane(  );
        cp.setLayout(new GridLayout(0, 1));    // one vertical column

        JButton theButton = new JButton("Change font");
        cp.add(theButton);

        final JLabel theLabel = new JLabel("Java is great!");
        cp.add(theLabel);

        // Now that theButton and theLabel are ready, make the action listener
        theButton.addActionListener(new ActionListener(  ) {
            public void actionPerformed(ActionEvent e) {
                fc.setVisible(true);
                Font myNewFont = fc.getSelectedFont(  );
                System.out.println("You chose " + myNewFont);
                theLabel.setFont(myNewFont);
                f.pack(  );    // again
                fc.dispose(  );
            }
        });

        f.pack(  );
        f.setVisible(true);
        f.addWindowListener(new WindowCloser(f, true));
    }
}
Font Chooser in action

Figure 13-13. Font Chooser in action

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.178.181