Advertisement



< Prev
Next >





Parameters passed to an applet





In this article, we will show you how to pass some parameters to an applet and how to read those parameters in an applet to display their values in the output.

Steps to accomplish this task -:
  • To pass the parameters to the Applet we need to use the param attribute of <applet> tag.
  • To retrieve a parameter's value, we need to use the getParameter() method of Applet class.





Signature of the getParamter() method


public String getParameter(String name)
  • Method takes a String argument name, which represents the name of the parameter which was specified with the param attribute in the <applet> tag.
  • Method returns the value of the name parameter(if it was defined) else null is returned.





  • Passing parameters to an applet.


    • In the upcoming code, we are going to pass a few parameters like Name, Age, Sport, Food, Fruit, Destination to the applet using param attribute in <applet>
    • Next, we will retrieve the values of these parameters using getParameter() method of Applet class.

    import java.awt.*;
    import java.applet.*;
    
    
    /* 
    <applet code="Applet8" width="400" height="200">
    <param name="Name" value="Roger">
    <param name="Age" value="26">
    <param name="Sport" value="Tennis">
    <param name="Food" value="Pasta">
    <param name="Fruit" value="Apple">
    <param name="Destination" value="California">
    </applet>
    */
      
      
    public class Applet8 extends Applet 
    {
    String name;
    String age;
    String sport;
    String food;
    String fruit;
    String destination;
    
    
    
    public void init()
    {
    name = getParameter("Name");
    age = getParameter("Age");
    food = getParameter("Food");
    fruit = getParameter("Fruit");
    destination = getParameter("Destination");
    sport = getParameter("Sport");
    }
    
    
    
    public void paint(Graphics g)
    {
    g.drawString("Reading parameters passed to this applet -", 20, 20);
    g.drawString("Name -" + name, 20, 40);
    g.drawString("Age -" + age, 20, 60);
    g.drawString("Favorite fruit -" + fruit, 20, 80);
    g.drawString("Favorite food -" + food, 20, 100);
    g.drawString("Favorite destination -" + name, 20, 120);
    g.drawString("Favorite sport -" + sport, 20, 140);
    }
    
    }
    


    Output


    In order to run our applet using the appletviewer, type the following command at the command prompt-

    appletviewer Applet8.java

    Where Applet8.java is the name of java file that contains the code of an applet. Right after running the applet program using appletviewer a new applet window is displayed to us -




    Advertisement




  • Passing a parameter to an applet to set a message on its status bar.

    • In the upcoming code, we are passing a parameter to the applet and setting the message on the status bar of the applet window with the value of this parameter.
    • showStatus() method of Applet class is called to set a message on the status bar of the applet window.

    
    
    import java.awt.*;
    import java.applet.*;
    
    /* 
    <applet code="Applet11" width="400" height="300">
    <param name="StatusBar" value="Have a good day">
    
    */
      
    public class Applet11 extends Applet 
    {
    String statusBar;
    
    
    public void init()
    {
    statusBar= getParameter("StatusBar");
    }
    
    
    public void paint(Graphics g)
    {
    g.drawString("Reading the applet parameter to set status bar message - ", 20, 20);
    
    //Setting a message at the status bar of our applet.
    showStatus(statusBar); 
    g.drawString("Setting the status bar message -" + statusBar, 20, 100);
    }
    }


    Output-


    In order to run our applet using appletviewer, type the following command at the command prompt-

    appletviewer Applet11.java

    Where Applet11.java is the name of java file that contains the code of an applet.


    • We have passed a parameter to an applet, named StatusBar with the value Have a good day.
    • Next, we have read the value of this parameter by calling getParameter() method and have used it to set the message at the status bar of an applet, by calling showStatus() method of Applet class.




    Please share this article -



    Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement

p