View RSS Feed

My Java Tips

Applets with parameters

Rate this Entry
by , 11-19-2011 at 06:37 PM (1063 Views)
Applets may accept parameters. This post briefly shows how to do that.

Applet in included in HTML using APPLET tag. You have to specify the parameters in APPLET tag in the following form:
Java Code:
<applet code="AppletSubclass.class" width="anInt" height="anInt">
<param name="parameter1Name" value="aValue"></param>
<param name="parameter2Name" value="anotherValue"></param>
Let me show you with an example:
Java Code:
<applet code="Animator.class" width="460" height="160&lt;br"></applet> ALT="If you could run this applet, you'd see some animation"&gt;
<param name="imageSource" value="images/Beans"></param>
<param name="backgroundColor" value="0xc0c0c0"></param>
<param name="endImage" value="10"></param>
<param name="soundSource" value="audio"></param>
<param name="soundtrack" value=""></param>
<param name="sounds"></param>       VALUE="|||||||8au||"&gt;
<param name="pause" value="200"></param>
Your browser is completely ignoring the APPLET tag!
Now, applet will be send parameters and in the applet, we have to get them and have to act accordingly. Applet code to get the parameters is as follows:
Java Code:
int requestedWidth = 0;
. . .
String windowWidthString = getParameter("WINDOWWIDTH");
if (windowWidthString != null) {
    try {
        requestedWidth = Integer.parseInt(windowWidthString);
    } catch (NumberFormatException e) {
        //Use default width.
Play around and explore.

Submit "Applets with parameters" to Facebook Submit "Applets with parameters" to Digg Submit "Applets with parameters" to Submit "Applets with parameters" to StumbleUpon Submit "Applets with parameters" to Google

Java SE