How to Use Parameters with a Java Applet

Using Java to Read an Applet Parameter

© Mark Alexander Bain

Feb 4, 2009
Pass Parameters to a Java Applet, Mark Alexander Bain
Parameters can be added to an Applet by using the PARAM HTML field. The trick is then how to use the parameter in a Java applet.

Java applications can be added into web pages by making use of applets. An applet is a Java application with some capabilities removed. This means, for example, that the application will not be able to access the user's file system (for obvious security reasons).

However, the web page designer will need to be able to pass information to the applet, and the simplest way to do that is to use applet parameters.

Adding Applet Parameters to a Web Page

An applet parameter is defined within a web page's HTML applet tag and is created by adding a param field, for example:

<applet width =100% height=40 code="paramHeading.class">
<param name="headingText" value="Use a Param as an Input to Java">
</applet>

Here the parameter has been given the name headingText and it has been given a value. Obviously the next thing to do is to use this within the Java code.

Using an Applet Parameter in a Java Class

The Java Application must be able to use the parameter that is being passed to it. To do this the Java programmer uses the getParameter method. For example, to load the contents of an applet parameter into a Java variable then the programmer could use the method in the following way:

String headingText;
public void init () {
headingText = getParameter("headingText");
}

This variable can now be used within the class, perhaps to output the text on the screen as part of the paint method:

public void paint (Graphics g) {
g.drawString(headingText, 20, 20);
}

However, there is an issue at this point. The parameter can only be read in as a string and not as a number. Fortunately the solution is, of course, very simple.

Working with Numbers in Applet Parameters

If the following code were to be used then it would result in an error:

String headingText;
int fontHeight = getParameter("fontHeight");

And that is because the applet parameter can only be read in as a string. Instead the programmer has to carry out the process in two steps:

  • read the applet param field in as a string
  • convert the string to a number

The code to do this would be something like:

String tmpfontHeight = getParameter("fontHeight");
int fontHeight = Integer.parseInt(tmpfontHeight);

The web page will now be able to pass any necessary information to the Java applet which, in turn, will be able to obtain and process the parameter data.

Summary

Data can be made available to a Java applet by adding a param field to the applet when it is defined on a web page. The Java applet can then access the parameter by using the getParameter method. This method can only read the parameter in as a string and therefore the programmer must carry out the appropriate conversion method so that the application can handle the number correctly. The parameter can then be used as required by the Java applet.

Working Examples

Working examples of the techniques show here can be found in Examples of How to Use Parameters with a Java Applet.

Further Reading

Anyone unsure of the techniques involved in creating a Java applet may also like to read How to Add a Java Applet to a Web Page and anyone wanting to move on to standalone applications may wish to read How to Create a Simple Java GUI.


The copyright of the article How to Use Parameters with a Java Applet in Javascript/Java Programming is owned by Mark Alexander Bain. Permission to republish How to Use Parameters with a Java Applet in print or online must be granted by the author in writing.


Pass Parameters to a Java Applet, Mark Alexander Bain
Java can Read the Param HTML Field, Mark Alexander Bain
A Simple Java Applet, Mark Alexander Bain
   


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo