Java Popup
Volume Number: 12
Issue Number: 7
Column Tag: Getting Started
A Java Popup Menu URL Launcher
By Dave Mark
Note: Source code files accompanying article are located on MacTech CD-ROM or
source code disks.
Since last month’s column, I’ve been consumed with Java. I’ve been reading every
Java book I could get my hands on, including the galleys for Learn Java on the
Macintosh (Addison-Wesley), Java in a Nutshell (O’Reilly & Associates), Active Java
(Addison-Wesley), Java Essentials for C and C++ Programmers (Addison-Wesley),
and Teach Yourself Java for Macintosh In 21 Days (Hayden). There are lots of Java
books out there, some of them much better than others.
Beware of Java books written to pre-release versions of the Java SDK from Sun.
Much has changed between the pre-release and release versions and, as a slew of
messages on the net will attest, those changes will frustrate you as you attempt to get
the book sample code to work.
The applets from Learn Java on the Macintosh and Java in a Nutshell have been
translated into Metrowerks’ Java project format. You can download the applets from
Metrowerks’ Web site (www.metrowerks.com).
This Month’s Applets
One important use of Java is to extend the functionality of a Web site, beyond the
capabilities offered by HTML. If you’ve ever been to Netscape’s Web site, you’ve
probably encountered the long list of download sites that allow you to download the
latest version of a Netscape product (Figure 1).
If you’ve got a long list of URLs to put on your Web site, you can, of course,
represent them just like Netscape did, as a long series of links. The downside to this
approach is that the links can take up a fair amount of screen space. An alternative is
to use a form CGI and represent the list as a popup menu of URLs.
Figure 1. The long list of sites to download the latest version of Netscape Navigator
Solving the problem using a CGI is fine, but I wanted to implement this “popup”
solution as a Java applet instead. To do this, we need to take advantage of the existing
Java applet model, as embodied in the java.applet.Applet class. To make this a
little easier to understand, I developed the applet in two stages. Here’s the first
version:
import java.applet.*;
import java.awt.*;
import java.net.*;
public class popup extends Applet
{
private Choice urlChoices;
int numURLs;
public void init()
{
this.setBackground( Color.yellow );//java.awt.Color
this.setForeground( Color.red );//java.awt.Color
urlChoices = new Choice();
urlChoices.addItem( "www.metrowerks.com" );
urlChoices.addItem( "www.netscape.com" );
urlChoices.addItem( "www.mactech.com" );
urlChoices.setForeground( Color.green );
urlChoices.setBackground( Color.blue );
this.add( new Label( "Select URL: " ) );
this.add( urlChoices );
}
public boolean action( Event e, Object obj )
{
URL url;
if ( e.target == urlChoices )
{
try
{
url = new URL( obj.toString() );
}
catch( MalformedURLException err )
{
// Could print error message here.
return true;
}
this.getAppletContext().showDocument( url );
return true;
}
else
return super.action( e, obj );
}
}
Since I’m using CodeWarrior, I started by creating a new project file called
popup.µ using the Java applet stationery. Next, I created a new source code file named
popup.java and added it to the project. I typed in my source code, saved it, then
selected the .java file in the project window and compiled it. The compiler used my
Java source code to produce a file named popup.class which contains the Java byte
code that will be loaded by my HTML file.
Speaking of my HTML file, I used CodeWarrior to create a new file called
popup.html and saved it in the same folder as my other project files. I didn’t add the
HTML file to the project. Here’s the HTML:
URL Launcher
This applet creates a popup menu of URLs.
R