SOURCE CODE for SiteSelector applet which provides buttons to different URLs
// load a document from a URL when a button is pressed
import java.applet.*;
import java.awt.*;
import java.net.*;
public class SiteSelector extends Applet {
Site siteList[]; // array of sites
public void init()
{
siteList = new Site[3];
siteList[0] = new Site ("Wayne's World", "http://csc.ColumbusState.edu/summers");
siteList[1] = new Site ("CS335 Web Programming", "http://rex.nmhu.edu");
siteList[2] = new Site ("NMHU Computer Science Department", "http://cs.nmhu.edu");
for (int i = 0; i < siteList.length; i++)
add( new Button (siteList[i].getTitle()) );
}
public boolean action( Event e, Object o)
{
if ( e.target instanceof Button )
{
String title;
URL location;
for (int i = 0; i < siteList.length; i++)
{
title = siteList[i].getTitle();
location = siteList[i].getLocation();
if (title.equals (e.arg.toString() ) )
{
gotoSite (location);
showStatus ("going to URL " + location);
return true;
}
}
}
return false; // event not handled yet
}
public void gotoSite (URL loc)
{
// this must be executed in a browser such as Netscape
getAppletContext().showDocument (loc);
}
}
class Site extends Button
{
private String title;
private URL location;
public Site (String siteTitle, String siteLocation)
{
title = siteTitle;
try
{ location = new URL (siteLocation); }
catch (MalformedURLException e)
{ System.err.println( "Invalid URL: " + siteLocation ); }
}
public String getTitle() { return title; }
public URL getLocation() { return location; }
}