Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Using the List Component

 

Training Index

by jGuru

Help  API Docs  About This Short Course  Short Course  Exercises

For this exercise you design an applet that displays a List object and a TextField object. When an item is selected from the list, the associated string is displayed in the noneditable textfield. The list is populated with the names of the currently recognized fonts.

Prerequisites

Skeleton Code

ListFonts.java uses an API that was superceded with the Java 2 platform, version 1.2. Use ListFonts.java for version Java 1.1, or ListFonts2.java for Java 2 platform, version 1.2. Notice the changes in the code:

// version 1.1
String fontList[] = getToolkit().getFontList(); 

// version 1.2
GraphicsEnvironment env =
GraphicsEnvironment.getLocalGraphicsEnvironment();
   String fontList[] = env.getAvailableFontFamilyNames();
   
// version 1.1
list.addItem(fontList[i]);

// version 1.2
list.add(fontList[i]);

Tasks

1.To receive the item events, add the applet itself as an ItemListener target. That is, the applet should implement this interface.
2.Define the itemStateChanged() method so that it calls getSelectedItem() to get the currently selected item and displays it in the textfield, tf, with setText().

Where help exists, the task numbers above are linked to the step-by-step help page.

Solution Source

Demonstration

This exercise only runs within browsers that support the Java Runtime Environment 1.1 and appletviewer. If you are using an appropriate browser, try this exercise. The output should look similar to the following:

Applet List Font Viewer

Next Exercise

Exercises

Short Course

Copyright 1996-2000 jGuru.com. All Rights Reserved.