JClass Elements

PreviousNextIndex

7

Font Choosers

Features of JCFontChooser and its Subclasses  Classes  Properties  Methods  Examples


7.1 Features of JCFontChooser and its Subclasses

JCFontChooser is the abstract base class for JCFontChooserBar and JCFontChooserPane. It provides common data and methods for both components.

Figure 30 :  A JCFontChooserBar and a JCFontChooserPane.


7.2 Classes

JCFontEvent

Used to inform listeners that the font has been changed. Its constants are:
JCFontEvent.FONT_NAME_CHANGE,
JCFontEvent.FONT_SIZE_CHANGE
JCFontEvent.BOLD_STYLE_CHANGE,
JCFontEvent.ITALIC_STYLE_CHANGE,

JCFontEvent.UNDERLINE_STYLE_CHANGE.

JCFontChooserBar

A GUI component suitable for a menu bar.

JCFontChooserPane

A GUI component suitable for a dialog or a tabbed pane.

JCFontListener

The listener interface. Methods are fontChanging and fontChanged.

JCFontAdapter

A convenience class that provides empty implementations of the listener interface's methods.


7.3 Properties

Properties of JCFontChooserBar and JCFontChooserPane

toolTipEnabled

A Boolean property that indicates whether Tool Tips are being used. The get method is called isToolTipEnabled.

selectedFont

The set method of this property has three different signatures: a single parameter Font font, a two parameter version, Font font, boolean underline, and a version for setting every font-related parameter, String name, int style, int size, boolean underline.

For a full listing of the properties, please see Properties of JCFontChooserBar and Properties of JCFontChooserPane in Appendix A.


7.4 Methods

Because the initial choice of font parameters is made in the constructor, and subsequent changes are made by interacting with the GUI, there are no public methods of interest in JCFontChooserBar or JCFontChooserPane. Only the listener methods need concern you.

You listen for font changes by implementing the JCFontListener interface. Its two methods are fontChanging(), and fontChanged(). Both methods take a JCFontEvent parameter. Use the first method to inspect and possibly veto the change in font, or in the underline state. Use the second to notify of these changes.

A JCFontEvent contains information about its source, the type of change that was made, old and new Font values, old and new underline values, and a Boolean fontChanging parameter that indicates whether this is a vetoable change or not.

Note: The "old" font and underline values are read-only.


7.5 Examples

In this example we'll add both a JCFontChooserBar and a JCFontChooserPane to the same panel. Normally, you place a JCFontChooserPane in its own dialog, but adding it to a JPanel as is done here doesn't change the way JCFontChooserPane's properties are set. The code snippet shows how to instantiate both components and how to add a JCFontListener so you can respond to font-changed events. Since the listening object is JCFontExample, it needs to provide an implementation of fontChanged, the method that is declared in interface JCFontListener. The JCFontChooserBar is added to a JToolbar, as is shown first.

public class JCFontExample extends JPanel implements JCFontListener {

...
bar = new JToolBar();
Font font3 = new Font("Serif", Font.PLAIN, 12);
font3 = JCFontChooser.setUnderline(font3, true);
...

fontBar = new JCFontChooserBar(font3);
bar.add(fontBar);
fontBar.addJCFontListener(this);
...
fontPane = new JCFontChooserPane(font3);
fontPane.addJCFontListener(this);
...
}

Figure 31 :  A bug notice in a JCFontChooserPane.

Please note that there is a problem with early Java 1.2 VMs that may require an extra block in your code. You may not see the changes in JCFontChooserPane's preview area unless you add the following block of code:

//================== JCFontListener interface methods

/** Font is changing. Listeners can change
*/ the font and/or underline indication. */
public void fontChanging(JCFontEvent e) {
}

/** Font has been changed. */
public void fontChanged(JCFontEvent e) {
System.out.println("Font changed to: "+ e.getFont());
  Object source = e.getSource();
if (source instanceof JCFontChooserBar || source instanceof
      JCFontChooserPane) {
    Font font = e.getFont();
    Container parent = sampleText.getParent();
  sampleText.setFont(font);
    sampleText.repaint();
}
}


PreviousNextIndex