// Fig. 16.1: SiteSelector.java // This program uses a button to load a document from a URL. import java.awt.*; import java.awt.event.*; import java.net.*; import java.util.Hashtable; import java.applet.Applet; public class SiteSelector extends Applet implements ItemListener { private Hashtable sites; private Choice siteChoice; public void init() { sites = new Hashtable(); siteChoice = new Choice(); // look in HTML for parameters and add sites to HashTable String title, location; URL url; int counter = 0; while ( true ) { title = getParameter( "title" + counter ); if ( title != null ) { location = getParameter( "location" + counter ); try { url = new URL( location ); sites.put( title, url ); siteChoice.add( title ); } catch ( MalformedURLException e ) { e.printStackTrace(); } } else break; ++counter; } add( new Label( "Choose a site to browse:" ) ); siteChoice.addItemListener( this ); add( siteChoice ); } public void itemStateChanged( ItemEvent e ) { URL url = (URL) sites.get( e.getItem() ); getAppletContext().showDocument( url ); } }