The JavaTM Tutorial
Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Trail: Writing Applets
Lesson: Taking Advantage of the Applet API

Deciding Which Parameters to Support

This page guides you through the four questions you should ask as you implement parameters:

It ends with a discussion of the parameters defined by an example class named AppletButton.

What Should the Applet Let the User Configure?

The parameters your applet should support depend on what your applet does and on how flexible you want it to be. Applets that display images might have parameters to specify the image locations. Similarly, applets that play sounds might have parameters to specify the sounds.

Besides parameters that specify resource locations (such as image and sound files), applets sometimes provide parameters for specifying details of the applet's appearance or operation. For example, an animation applet might let the user specify the number of images shown per second. Or an applet might let the user change the strings the applet displays. Anything is possible.

What Should the Parameters Be Named?

Once you decide what parameters your applet will support, you need to figure out their names. Here are some typical parameter names:
SOURCE or SRC
For a data file such as an image file.
XXXSOURCE (for example, IMAGESOURCE)
Used in applets that let the user specify more than one type of data file.
XXXS
For a parameter that takes a list of XXXs (where XXX might be IMAGE, again).
NAME
Used only for an applet's name. Applet names are used for interapplet communication, as described in Sending Messages to Other Applets. ) .

Clarity of names is more important than keeping the name length short. Do not use names of <APPLET> tag attributes, which are documented in The <APPLET> Tag.


Note:  Although this tutorial usually refers to parameter names using ALL UPPERCASE, parameter names are case-insensitive. For example, IMAGESOURCE and imageSource both refer to the same parameter. Parameter values, on the other hand, are case-sensitive unless you take steps interpret them otherwise, such as by using the String toLowerCase method before interpreting the parameter's value.

What Kind of Value Should Each Parameter Take?

Parameter values are all strings. Whether or not the user puts quotation marks around a parameter value, that value is passed to your applet as a string. However, your applet can interpret the string in many ways.

Applets typically interpret a parameter value as one of the following types:

What Should the Default Value of Each Parameter Be?

Applets should attempt to provide useful default values for each parameter, so that the applet will execute even if the user doesn't specify a parameter or specifies it incorrectly. For example, an animation applet should provide a reasonable setting for the number of images it displays per second. This way, if the user doesn't specify the relevant parameter, the applet will still work well.

An Example: AppletButton

Throughout this tutorial, applets that need to bring up windows use the highly configurable AppletButton (in a .java source file) class.

Note:  If you're using 1.1 or 1.2, you should look at the Swing version of AppletButton.java (in a .java source file).
One page alone -- Using Layout Managers (in the Writing Applets trail) -- uses AppletButton five times for five different examples, one per layout manager the AWT provides. AppletButton's GUI is simple, consisting of a button and a label that displays status. When the user clicks the button, the applet brings up a window.

The AppletButton class is so flexible because it defines parameters that let the user specify any or all of the following:

Here's what a typical <APPLET> tag for AppletButton looks like. You can see this applet running at the URL Using Layout Managers (in the Writing Applets trail).

<APPLET CODE=AppletButton.class CODEBASE=example
     WIDTH=350 HEIGHT=60>
<PARAM NAME=windowClass VALUE=BorderWindow>
<PARAM NAME=windowTitle VALUE="BorderLayout">
<PARAM NAME=buttonText
       VALUE="Click here to see a BorderLayout in action">
</APPLET>

When the user doesn't specify a value for a parameter, AppletButton uses a reasonable default value. For example, if the user doesn't specify the window's title, AppletButton uses the window's type as the title.

The next page shows you the code AppletButton uses to get its parameter values from the user.


Previous Page Lesson Contents Next Page Start of Tutorial > Start of Trail > Start of Lesson Search
Feedback Form

Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.