Most read:
Popular archives:
Java Q&A Forums - Let the great migration begin
We're pleased to announce the first phase of the integration of the Java Q&A Forums with our community platform, JavaWorld's
Daily Brew. Whether you're one of our longtime forum users or a brand newbie, we hope you'll visit the Java Q&A Forums in their new home alongside JW Blogs.
| Enterprise AJAX - Transcend the Hype |
| Oracle Compatibility Developer's Guide |
init() method that will fetch fields from strings returned by the getParameter() method.This article presents an elegant method for applet parameterization. Paramterization means passing parameters via PARAM tags embedded in the HTML file. This method does all the parameter fetching
for you. It may be quite a while before you ever need to type getParameter again! The article assumes you have a reasonably good knowledge of the class reflection mechanism, although you could probably
figure it out by just looking at the code.
The class reflection mechanism feature is located in the java.lang.reflect package. It lets you examine the structure of a class and provides runtime information on fields, methods, access modifiers,
and so on.
Most Java programmers know that, ideally, applets should be configurable from their corresponding HTML document -- but how many really are? For my own part, when I neglect to define parameters that would allow Web designers to feed my applets with data of their own, it's usually because I just don't feel like writing an endless sequence of lines that look like this:
// Get delay (in frames) between billboard messages.
param = parent.getParameter("BILLBOARD_DELAY");
if (param != null) {
try {
BILLBOARD_DELAY = Integer.parseInt(param);
}
catch (Exception e) {}
}
// Get checker cell dimension.
param = parent.getParameter("CHECKER_CELL_WIDTH");
if (param != null) {
try {
CHECKER_CELL_WIDTH = Float.valueOf(param).floatValue();
}
catch (Exception e) {}
}
// Gravitational constant.
param = parent.getParameter("G");
if (param != null) {
try {
G = Float.valueOf(param).floatValue();
}
catch (Exception e) {}
}
...
Even if you discipline yourself in parameterizing right from the beginning stages of your development, there will always be
a number of fields that will either appear, disappear, or simply change type during the course of development. This means
going back to the init() method each time such alterations occur. Having to do this is really annoying because it's very useful to test various settings
by updating the parameters directly from the HTML page during the development stage. You don't want to leave parameterization
until the final stages of your development -- if you do, you'll have to hardcode values and compile each time you want to
test new settings.
This article describes a method that does all the parameter fetching for you and may save you from having to type getParameter ever again!
Our parameter fetching method initializes every public non-final field that has a corresponding PARAM tag in the HTML document. If you only want a subset of those fields to be fetched, you have to adopt a field-naming convention where each field to be included must have a name that begins with a prefix of your liking. You usually won't have to use this scheme. I included this feature in case you have public fields you want to hide from the HTML coder.