FileDocCategorySizeDatePackage
FontChooser.javaAPI DocExample5811Sat Mar 20 14:44:56 GMT 2004None

FontChooser

public class FontChooser extends JDialog
A font selection dialog.

Note: can take a long time to start up on systems with (literally) hundreds of fonts. TODO change list to JList, add a SelectionChangedListener to preview.

author
Ian Darwin
version
$Id: FontChooser.java,v 1.19 2004/03/20 20:44:56 ian Exp $

Fields Summary
protected Font
resultFont
The font the user has chosen
protected String
resultName
The resulting font name
protected int
resultSize
The resulting font size
protected boolean
isBold
The resulting boldness
protected boolean
isItalic
The resulting italicness
protected String
displayText
Display text
protected String[]
fontList
The list of Fonts
protected List
fontNameChoice
The font name chooser
protected List
fontSizeChoice
The font size chooser
Checkbox
bold
The bold and italic choosers
Checkbox
italic
protected String[]
fontSizes
The list of font sizes
protected static final int
DEFAULT_SIZE
The index of the default size (e.g., 14 point == 4)
protected JLabel
previewArea
The display area. Use a JLabel as the AWT label doesn't always honor setFont() in a timely fashion :-)
Constructors Summary
public FontChooser(Frame f)
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.


	                            	 
	   
		super(f, "Font Chooser", true);

		Container cp = getContentPane();

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

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

		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++)
			fontNameChoice.add(fontList[i]);
		fontNameChoice.select(0);

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

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

		cp.add(top, BorderLayout.NORTH);

		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(displayText, JLabel.CENTER);
		previewArea.setSize(200, 50);
		cp.add(previewArea, BorderLayout.CENTER);

		Panel bot = new Panel();

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

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

		JButton canButton = new JButton("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(bot, BorderLayout.SOUTH);

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

		pack();
		setLocation(100, 100);
	
Methods Summary
public java.awt.FontgetSelectedFont()
Retrieve the selected font, or null

		return resultFont;
	
public java.lang.StringgetSelectedName()
Retrieve the selected font name.

		return resultName;
	
public intgetSelectedSize()
Retrieve the selected size

		return resultSize;
	
public static voidmain(java.lang.String[] args)
Simple main program to start it running

		final JFrame f = new JFrame("FontChooser Startup");
		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!", JLabel.CENTER);
		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();		// adjust for new size
				fc.dispose();
			}
		});

		f.setSize(150, 100);
		f.setVisible(true);
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	
protected voidpreviewFont()
Called from the action handlers to get the font info, build a font, and set it.

		resultName = fontNameChoice.getSelectedItem();
		String resultSizeName = fontSizeChoice.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.