©1994-2003 Kevin Boone
Home     Section index     K-Zone home Simple Java programs

Site search

Glossary
Confused by computer jargon? Look it up!

Shameless plug


Now available!

Articles
- Ten-minute guide to setting up a WAP site

- Talk like your boss: new developments in managerese

More...

Development
File handling in the Linux kernel

Java development for the Sony-Ericsson P800

SunONE Application Server 7 FAQ

More...

Linux
Using Linux with the Treo 600

- Linux on the Tecra M1

- Some notes on openzaurus

More...

Download
Java stuff

Linux stuff

More...

(Please read the download policy)

Home automation
The X10 system

Linux TW723 driver

More...

The K-Zone
K-Zone computing

K-Zone law

K-Zone education and science

K-Zone motorcycles

K-Zone DIY

K-Zone railways

K-Zone martial arts

About the author

K-Zone home page

 
Software development
Computing
Hello1.java
Add1.java
Add1_1.java
AwtTest.java
Button1.java
Button2.java
Calculator.java
CalculatorApp.java
ClockApplet.java
CountSpace1.java
DateTime.java
Div1.java
DoNothing.java
Factorial1.java
Factorial2.java
IfTest.java
Inheritance1.java
Loan.java
DiceThrower.java
MultiplicationTable.java
Queue.java
Reverse1.java
SoundApplet.java
Stressometer.java
TextReaderTest.java
WebMerge.java
Zener.java
`FontChooser' example program: selects fonts; demonstrates AWT principles

FontChooser

/* 
FontChooser.java

This program displays the list of font typefaces available on the system.
(I suppose technically it should be called `TypefaceChooser'). When the
user clicks on the name of a typeface, it displays some text in that
typeface. This is a quite a useful program to have, as it can assist
in the selection of fonts for a program's display.

The display looks like this:



(Note that you will only see the images if you are looking at the HTML version of this
file, FontChooser.java.html).

Kevin Boone, August 1999
*/

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

/*
class FontChooser 
This program has only one class, which is a subclass of Applet as usual.
Note that we define the class to `implememts ItemListener'. This
specifies it as being able to receive events from the list component.
When the user clicks on an item in the list, this program receives
an ItemEvent event. This causes the operation itemStateChange to
be executed. 
*/

public class FontChooser extends Applet implements ItemListener
{
/*
The display consists of two parts: the list of fonts (fontList) and the
font display area (fontDisplayLabel). Since the display shows text, it is
implemented as a label.

These objects have to be stored as attribtues, as they are created in
the constructor but used in the operation itemStateChange.
*/
Label fontDisplayLabel;
List fontList;

/*
FontChooser constructor
*/

public FontChooser()
	{
	super();
	// A BorderLayout is probably the right thing to use here. We will put the main
	//  part of the display (the font list) in the middle, and the text at the top
	setLayout(new BorderLayout());

	// This very ugly line retrieves the list of font typefaces as an array of Strings
	String listOfFonts[] = 
		GraphicsEnvironment.getLocalGraphicsEnvironment().getAvailableFontFamilyNames();
	
	// Create the font list object, and put the names of the typefaces in it
	fontList = new List();
	for (int i = 0; i < listOfFonts.length; i++)
		fontList.add(listOfFonts[i]);
	add(fontList, BorderLayout.CENTER);	

	// We now use addItemListener to specify that `this' (that is, this applet)
	//  will handle events from the list
	fontList.addItemListener(this);

	// Now create the font display area. It starts of empty, as the user has not
	//  selected a font yet
	fontDisplayLabel = new Label("");
	add(fontDisplayLabel, BorderLayout.NORTH);
	}

/*
itemStateChanged
This operation is called whenever the user clicks on a font name (that is, any
item in the font list
*/

public void itemStateChanged (ItemEvent e)
	{
	// Only proceed if an item is being selected. This operation is also
	//  called for the item that is being de-selected. We don't want
	//  todo anything special about that
	if (e.getStateChange() == ItemEvent.SELECTED)
		{
		// Get the name of the typeface from the list
		String fontName = fontList.getSelectedItem();

		// Create a font with that name
		Font font = new Font(fontName, Font.PLAIN, 12);
		
		// Set the font display label to the selected
		//  font, and insert the name as its text
		fontDisplayLabel.setText(fontName);
		fontDisplayLabel.setFont(font);
		}
	}
}