Java allows users to pass user-defined parameters to an applet with the help of <PARAM>tags. The <PARAM>tag has a NAME attribute which defines the name of the parameter and a VALUE attribute which specifies the value of the parameter. In the applet source code, the applet can refer to the parameter by its NAME to find its value. The syntax of the <PARAM>tag is:
<APPLET>
<PARAMNAME=parameter1_name VALUE=parameter1_value>
<PARAMNAME=parameter2_name VALUE=parameter2_value>
<PARAMNAME=parametern_name VALUE=parametern_value>
</APPLET>
For example, consider the following statements to set the text attribute of applet to This is an example of Parameter! ! !
<APPLET>
<PARAMNAME=text VALUE=This is an example of Parameter!!!>
</APPLET>
Note that the <PARAM>tags must be included between the <APPLET> and</ APPLET> tags. The init () method in the applet retrieves user-defined values of the parameters defined in the <PARAM>tags by using the get Parameter () method. This method accepts one string argument that holds the name of the parameter and returns a string which contains the value of that parameter. Since it returns String object, any data type other than String must be converted into its corresponding data type before it can be used.
import java.awt.*;
import java.applet.*;
/*
<applet code="ParamApplet" width=300 height=80>
<paramname=fontName value=Courier>
<param name=fontSize value=14>
<param name=leading value=2>
<param name=accountEnabled value=true>
</applet>
*/
public class ParamApplet extends Applet
{
String name;
int size;
float lead;
boolean active;
public void start()
{
String pa;
name = getParameter("Name");
if(name == null)
name = "Not Found";
pa = getParameter("Size");
try
{
if(pa != null)
size = Integer.parseInt(pa);
else
size = 0;
}
catch(NumberFormatException e)
{
size = -1;
}
pa = getParameter("lead");
try
{
if(pa != null)
lead = Float.valueOf(pa).floatValue();
else
lead = 0;
}
catch(NumberFormatException e)
{
lead = -1;
}
pa = getParameter("AccountEnabled");
if(pa != null)
active = Boolean.valueOf(pa).booleanValue();
}
public void paint (Graphics g)
{
g.drawString("Name: " + name, 0, 10);
g.drawString("Size: " + size, 0, 26);
g.drawString("Leading: " + lead, 0, 42);
g.drawString("Account Active: " + active, 0, 58);
}
}