    Contents    Previous    Next

#### Writing an Applet

# Getting Attribute Values

You need to determine which applet-specific attributes -- name-value pairs specified in the `APP` HTML tag -- your applet will interpret. Attributes let your applet's users customize the execution of the applet. Attributes can also be useful for debugging applets.

Applets get the values of attributes using the getAttribute() method of the Applet class, as shown in this code example. (For a hint on viewing code examples, see the Code Examples page.) This example reads the value of an applet-specific attribute called COLOR, which can have a value of "white" or "red", and displays a string in that color.

Here's an example of including the applet, specifying "red" for COLOR:

        <APP CLASS="AppWithAttribute" COLOR="red">

Here (if you're using HotJava) is the result:

The getAttribute() method returns a String, which you can convert to other formats. For example, to convert an attribute value into a float:

        float someFloat;
        String attributeValue = getAttribute("SOMEFLOAT");
        if (attributeValue != null)
            someFloat = Float.valueOf(attributeValue).floatValue();

Some attributes are standard. CLASS, SRC, ALIGN, WIDTH, and HEIGHT all have special meanings to the `APP` HTML tag and shouldn't be used in other ways. You should also try not to use other HTML tags and attributes in ways inconsistent with their common use (see A Beginner's Guide to HTML, provided by NCSA).

Note: Attributes are case-insensitive, but attribute values are case-sensitive by default. For example, the user can use either color or COLOR for the attribute name, but (as written) the example applet accepts "red" but not "RED" for the attribute value.

    Contents    Previous    Next