Nam Voth
Posts: 1
Nickname: oliver7
Registered: Mar, 2002
|
|
AppletToDisplayWebPage
|
Posted: Mar 6, 2002 11:54 AM
|
|
This prog is from Java How to prog(Deitel & Deitel-4th Edition).The html codes followed are to display the SiteSelector applet. Why does the applet display in Applet Viewer when I run the html file from there but when I use Internet Explorer or Netscape to display the webpage it won't. Can anyone help me with this. Many thanks in advance
Nam
import java.net.*; import java.util.*; import java.awt.*; import java.applet.AppletContext; import java.applet.*;
// Java extension packages import javax.swing.*; import javax.swing.event.*;
public class SiteSelector extends JApplet { private Hashtable sites; // site names and URLs private Vector siteNames; // site names private JList siteChooser; // list of sites to choose from
// read HTML parameters and set up GUI public void init() { // create Hashtable and Vector sites = new Hashtable(); siteNames = new Vector();
// obtain parameters from HTML document getSitesFromHTMLParameters();
// create GUI components and layout interface Container container = getContentPane(); container.add( new JLabel( "Choose a site to browse" ), BorderLayout.NORTH );
siteChooser = new JList( siteNames );
siteChooser.addListSelectionListener(
new ListSelectionListener() {
// go to site user selected public void valueChanged( ListSelectionEvent event ) { // get selected site name Object object = siteChooser.getSelectedValue();
// use site name to locate corresponding URL URL newDocument = ( URL ) sites.get( object );
// get reference to applet container AppletContext browser = getAppletContext();
// tell applet container to change pages browser.showDocument( newDocument ); }
} // end anonymous inner class
); // end call to addListSelectionListener
container.add( new JScrollPane( siteChooser ), BorderLayout.CENTER );
} // end method init
// obtain parameters from HTML document private void getSitesFromHTMLParameters() { // look for applet parameters in the HTML document // and add sites to Hashtable String title, location; URL url; int counter = 0;
// obtain first site title title = getParameter( "title" + counter );
// loop until no more parameters in HTML document while ( title != null ) {
// obtain site location location = getParameter( "location" + counter ); // place title/URL in Hashtable and title in Vector try { // convert location to URL url = new URL( location );
// put title/URL in Hashtable sites.put( title, url ); // put title in Vector siteNames.add( title ); }
// process invalid URL format catch ( MalformedURLException urlException ) { urlException.printStackTrace(); }
++counter;
// obtain next site title title = getParameter( "title" + counter );
} // end while
} // end method getSitesFromHTMLParameters
} // end class SiteSelector
this is the html file:
<html> <title>Site Selector</title> <body> <applet code="SiteSelector.class" width="300" height="75"> <param name="title0" value="Java Home Page"> <param name="location0" value="http://java.sun.com/"> <param name="title1" value="Deitel"> <param name="Location1" value="http://www.deitel.com/"> <param name="title2" value="JGuru"> <param name="location2" value="http://www.jGuru.com/"> <param name="title3" value="JavaWord"> <param name="location3" value="http://www.javaworld.com">
</applet> </body> </html>
|
|