Java Reference
In-Depth Information
Discussion
As we saw in Drawing Text , 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);
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 14-12 ; 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 14-17 .
Example 14-12. FontChooser.java
// package com.darwinsys.swingui;
public
public class
class FontChooser
FontChooser extends
extends JDialog {
private
private static
static final
final long
long serialVersionUID = 5363471384675038069L ;
public
public static
static final
final String DEFAULT_TEXT = "Lorem ipsem dolor" ;
// Results:
/** The font the user has chosen */
protected
protected Font resultFont = new
new Font ( "Serif" , Font . PLAIN , 12 );
/** The resulting font name */
protected
protected String resultName ;
/** The resulting font size */
protected
protected int
int resultSize ;
/** The resulting boldness */
protected
protected boolean
boolean isBold ;
/** The resulting italicness */
protected
protected boolean
boolean isItalic ;
// Working fields
/** Display text */
protected
protected String displayText = DEFAULT_TEXT ;
Search WWH ::




Custom Search