/* Openers.java * a swing applet for loading a document from a URL into a browser * Serena Fenton - inls 150 - spring 2001 * implemented from Deitel & Deitel How to Program Java- example 21.1 * */ import java.net.*; import java.util.*; import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.applet.AppletContext; public class Openers extends JApplet { private Hashtable sites; private Vector siteNames; public void init() { sites = new Hashtable(); siteNames = new Vector(); getSitesFromHTMLParameters(); Container c = getContentPane(); c.add( new JLabel( "What would you like to draw?" ), BorderLayout.NORTH ); final JList siteChooser = new JList( siteNames ); siteChooser.addListSelectionListener( new ListSelectionListener() { public void valueChanged( ListSelectionEvent e ) { Object o = siteChooser.getSelectedValue(); URL newDocument = (URL) sites.get( o ); AppletContext browser = getAppletContext(); browser.showDocument( newDocument ); } } ); c.add( new JScrollPane( siteChooser ), BorderLayout.CENTER ); } private void getSitesFromHTMLParameters() { // look for applet parameters in the HTML document // 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 ); siteNames.addElement( title ); } catch ( MalformedURLException e ) { e.printStackTrace(); } } else break; ++counter; } } }