Java Applet Parameter Example

      No Comments on Java Applet Parameter Example

Java Applet Parameter Example

This Program about Java Applet Parameter demonstrate how to pass values using parameter. This will be very useful when you would like to specify some special value for some task. Lets discuss above example in details that here I have passed two numbers using parameter. I would like to do simple sum of the two numbers and would like to display in Applet.  You can pass any kind of parameters as per your requirement and you can use this value in your java applet program.

Did you know, Java Application Vs Java Applet

import java.applet.*;
import java.awt.*;

/*
	<applet code="Applet3.class" height="300" width="300">
		<param name="no1" value="10">
		<param name="no2" value="20">
	</applet>
*/
public class Applet3 extends Applet 
{

	public void paint(Graphics g) 
	{
		int n1 = Integer.parseInt(this.getParameter("no1"));
		int n2 = Integer.parseInt(this.getParameter("no2"));

		String s = "Java Applet Parameter Example : sum of n1 and n2 is :" + (n1+n2);
		g.drawString( s,20,20);
	}
}

 

Most important point to remember here is that :

  • To pass extra value from html file you have to use param tag inside Applet tag.
  • To receive parameter value you need to use getParameter() method in Applet
  • Be sure that it will always send your values as String, so you need to convert it into your appropriate datatype before processing.

Leave a Reply