by Dinesh Thakur Category: AWT and Applets

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);
        }
}

 ParamApplet Output





About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles