|
||||||
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 PageAn 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 ClassThe 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 ParametersIf 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:
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. SummaryData 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 ExamplesWorking examples of the techniques show here can be found in Examples of How to Use Parameters with a Java Applet. Further ReadingAnyone 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.
|
||||||
|
|
||||||
|
|
||||||