1 /* 2 * File: ListTest.java 3 * 4 * Create a scrolling list that allows only one item to be selected. 5 * Uses ItemEvents when an item is selected with a "single click". 6 * Uses ActionEvents when an item is selected with a "double click". 7 * Also demonstrates the Toolkit class to get Font information from 8 * the browser. 9 * 10 * Copyright: Northeast Parallel Architectures Center 11 * 12 */ 13 14 import java.awt.List; 15 import java.awt.Label; 16 import java.awt.Color; 17 import java.awt.Toolkit; 18 import java.awt.Font; 19 import java.awt.event.ItemListener; 20 import java.awt.event.ActionListener; 21 import java.awt.event.ItemEvent; 22 import java.awt.event.ActionEvent; 23 24 public class ListTest extends java.applet.Applet 25 implements ItemListener, ActionListener { 26 27 // Allow five visible items in the list, but 28 // disallow multiple selections: 29 private List fontList = new List( 5, false ); 30 31 private String fontString; 32 private Label fontLabel; 33 private Font f = new Font ("Dialog", Font.PLAIN, 18); 34 35 public void init() { 36 setBackground( Color.white ); 37 setFont (f); 38 39 // Color the scrolling list: 40 Color lightBlue = new Color( 0xB0, 0xE0, 0xE6 ); 41 Color navyBlue = new Color( 0x19, 0x19, 0x70 ); 42 fontList.setBackground( lightBlue ); 43 fontList.setForeground( navyBlue ); 44 45 // Get list of system fonts: 46 Toolkit toolkit = Toolkit.getDefaultToolkit(); 47 String fonts[] = toolkit.getFontList(); 48 49 // Add fonts to the scrolling list: 50 for ( int i = 0; i < fonts.length; i++ ) { 51 fontList.addItem( fonts[i] ); 52 } 53 54 // Construct a text label: 55 fontString = " Once in a galaxy "; 56 fontString += "far, far away! "; 57 fontLabel = new Label( fontString, Label.CENTER ); 58 59 add( fontList ); 60 add( fontLabel ); 61 62 // Determine default font: 63 String defaultFontName = fontLabel.getFont().getName(); 64 65 // Select default font in scrolling list: 66 for ( int i = 0; i < fonts.length; i++ ) { 67 if ( defaultFontName.equals( fonts[i] ) ) { 68 fontList.select(i); break; 69 } 70 } 71 72 // Register the applet to listen for events: 73 fontList.addItemListener( this ); 74 fontList.addActionListener( this ); 75 } 76 77 public void itemStateChanged( ItemEvent event ) { 78 String fontName = fontList.getSelectedItem(); 79 int style = Font.PLAIN; 80 int size = fontLabel.getFont().getSize(); 81 fontLabel.setFont( new Font( fontName, style, size ) ); 82 fontLabel.setText( fontString ); 83 showStatus( "" ); 84 } 85 86 public void actionPerformed( ActionEvent event ) { 87 String fontName = fontList.getSelectedItem(); 88 int style = Font.BOLD; 89 int size = fontLabel.getFont().getSize(); 90 fontLabel.setFont( new Font( fontName, style, size ) ); 91 fontLabel.setText( fontString ); 92 showStatus( "You chose " + fontName + "!" ); 93 } 94 95 }