![]() ![]() ![]() |
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 forJCFontChooserBar
andJCFontChooserPane
. It provides common data and methods for both components.
Figure 30 : A JCFontChooserBar and a JCFontChooserPane.
- Constructors let you specify what the default fonts and sizes are, as well as letting you set whether underlining is on.
JCFontChooserPane
- provides a pane of controls designed to allow a user to manipulate and select a font. It is suitable for use in a tab pane or a dialog window.JCFontChooserPane
includes a preview area with sample text.JCFontChooserBar
provides a pane of controls designed to allow a user to manipulate and select a font. It is suitable for use in aJToolbar
.- Like the standard Swing components,
JCFontChooserBar
provides for the optional use of a Tool Tip.
7.2 Classes
7.3 Properties
Properties of JCFontChooserBar and JCFontChooserPane
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
orJCFontChooserPane
. Only the listener methods need concern you.You listen for font changes by implementing the
JCFontListener
interface. Its two methods arefontChanging()
, andfontChanged()
. Both methods take aJCFontEvent
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 BooleanfontChanging
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
public class JCFontExample extends JPanel implements JCFontListener {JCFontChooserBar
and aJCFontChooserPane
to the same panel. Normally, you place aJCFontChooserPane
in its own dialog, but adding it to aJPanel
as is done here doesn't change the wayJCFontChooserPane
's properties are set. The code snippet shows how to instantiate both components and how to add aJCFontListener
so you can respond to font-changed events. Since the listening object isJCFontExample
, it needs to provide an implementation offontChanged
, the method that is declared in interfaceJCFontListener
. TheJCFontChooserBar
is added to aJToolbar
, as is shown first.
...
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
//================== JCFontListener interface methodsJCFontChooserPane
's preview area unless you add the following block of code:
/** 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();
}
}
![]() ![]() ![]() |