/* * File: ListTest.java * * Create a scrolling list that allows only one item to be selected. * Uses ItemEvents when an item is selected with a "single click". * Uses ActionEvents when an item is selected with a "double click". * Also demonstrates the Toolkit class to get Font information from * the browser. * * Copyright: Northeast Parallel Architectures Center * */ import java.awt.List; import java.awt.Label; import java.awt.Color; import java.awt.Toolkit; import java.awt.Font; import java.awt.event.ItemListener; import java.awt.event.ActionListener; import java.awt.event.ItemEvent; import java.awt.event.ActionEvent; public class ListTest extends java.applet.Applet implements ItemListener, ActionListener { // Allow five visible items in the list, but // disallow multiple selections: private List fontList = new List( 5, false ); private String fontString; private Label fontLabel; private Font f = new Font ("Dialog", Font.PLAIN, 18); public void init() { setBackground( Color.white ); setFont (f); // Color the scrolling list: Color lightBlue = new Color( 0xB0, 0xE0, 0xE6 ); Color navyBlue = new Color( 0x19, 0x19, 0x70 ); fontList.setBackground( lightBlue ); fontList.setForeground( navyBlue ); // Get list of system fonts: Toolkit toolkit = Toolkit.getDefaultToolkit(); String fonts[] = toolkit.getFontList(); // Add fonts to the scrolling list: for ( int i = 0; i < fonts.length; i++ ) { fontList.addItem( fonts[i] ); } // Construct a text label: fontString = " Once in a galaxy "; fontString += "far, far away! "; fontLabel = new Label( fontString, Label.CENTER ); add( fontList ); add( fontLabel ); // Determine default font: String defaultFontName = fontLabel.getFont().getName(); // Select default font in scrolling list: for ( int i = 0; i < fonts.length; i++ ) { if ( defaultFontName.equals( fonts[i] ) ) { fontList.select(i); break; } } // Register the applet to listen for events: fontList.addItemListener( this ); fontList.addActionListener( this ); } public void itemStateChanged( ItemEvent event ) { String fontName = fontList.getSelectedItem(); int style = Font.PLAIN; int size = fontLabel.getFont().getSize(); fontLabel.setFont( new Font( fontName, style, size ) ); fontLabel.setText( fontString ); showStatus( "" ); } public void actionPerformed( ActionEvent event ) { String fontName = fontList.getSelectedItem(); int style = Font.BOLD; int size = fontLabel.getFont().getSize(); fontLabel.setFont( new Font( fontName, style, size ) ); fontLabel.setText( fontString ); showStatus( "You chose " + fontName + "!" ); } }