Passing Parameter to Applets with example

Java applet has the feature of retrieving the parameter values passed from the html page. So, you can pass the parameters from your html page to the applet embedded in your page. The param tag(<parma name="" value=""></param>) is used to pass the parameters to an applet. For the illustration about the concept of applet and passing parameter in applet


The applet has to call the getParameter() method supplied by the java.applet.Applet parent class. Calling getParameter("color") using the previous Java applet example would return a String value containing the text "blue". It is then left up to the applet to take advantage of this information and actually paint the text blue on the screen.
Here are three methods commonly used by applets:
• String getParameter(String name)-Returns the value for the specified parameter string
• URL getCodeBase()-Returns the URL of the applet
• URL getDocumentBase()-Returns the URL of the document containing the applet  



Program hai.java


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


  /*<APPLET code="hai" width="300" height="250">
<PARAM name="Message" value="Hai friend how are you ..?"></APPLET>*/
         
public class hai extends Applet {

  private String defaultMessage = "Hello!";

  public void paint(Graphics g) {
 
    String inputFromPage = this.getParameter("Message");
    if (inputFromPage == null) inputFromPage = defaultMessage;
    g.drawString(inputFromPage, 50, 55);
  
  }
 
}
Output 


Passing Parameter to Applets with example





You only need to change the HTML, not the Java source code. PARAMs let you customize applets without changing or recompiling the code.
This applet is very similar to the HelloWorldApplet. However rather than hardcoding the message to be printed it's read into the variable inputFromPage from a PARAM element in the HTML.
You pass getParameter() a string that names the parameter you want. This string should match the name of a PARAM element in the HTML page. getParameter() returns the value of the parameter. All values are passed as strings. If you want to get another type like an integer, then you'll need to pass it as a string and convert it to the type you really want.

The PARAM element is also straightforward. It occurs between <APPLET> and </APPLET>. It has two attributes of its own, NAME and VALUE. NAME identifies which PARAM this is. VALUE is the string value of the PARAM. Both should be enclosed in double quote marks if they contain white space.
An applet is not limited to one PARAM. You can pass as many named PARAMs to an applet as you like. An applet does not necessarily need to use all the PARAMs that are in the HTML. Additional PARAMs can be safely ignored.

3 comments:

Unknown said...

"Great blog created by you. I read your blog, its best and useful information. You have done a great work. Super blogging and keep it up.php jobs in hyderabad.
"

hahncabbage said...

trekz titanium headphones | Titanium Arts
Tinted in black metal, the Tinted headphones are a great choice for both casual titanium phone case and serious game pure titanium earrings players. titanium muzzle brake One of the grade 23 titanium best design options available in 2016 ford focus titanium a

senar said...

a658x6ihife640 wholesale sex toys,huge dildos,sex chair,women sex toys,sex chair,realistic dildo,dildo,realistic dildo,women sexy toys v824d8nnshv623

Post a Comment

 

learn java programming Copyright © 2011-2012 | Powered by appsackel.org