The Artima Developer Community
Sponsored Link

Java Answers Forum
AppletToDisplayWebPage

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Nam Voth

Posts: 1
Nickname: oliver7
Registered: Mar, 2002

AppletToDisplayWebPage Posted: Mar 6, 2002 11:54 AM
Reply to this message Reply
Advertisement
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>

Topic: sorting arrays Previous Topic   Next Topic Topic: ClassNotFoundException: xx_Stub with JBuilder5 IDE but fine in DOS prompt

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use