Start of Tutorial > Start of Trail > Start of Lesson | Search |
Applets use theApplet getParameter
method to get user-specified values for applet parameters. ThegetParameter
method is defined as follows:public String getParameter(String name)Your applet might need to convert the string that
getParameter
returns into another form, such as an integer. Thejava.lang
package provides classes such as Integer that you can use to help with converting strings to primitive types. Here's an example from theAppletButton
class of converting a parameter's value into an integer:Note that if the user doesn't specify a value for theint requestedWidth = 0; . . . String windowWidthString = getParameter("WINDOWWIDTH"); if (windowWidthString != null) { try { requestedWidth = Integer.parseInt(windowWidthString); } catch (NumberFormatException e) { //Use default width. } }WINDOWWIDTH
parameter, the above code uses a default value of 0, which the applet interprets as "use the window's natural size." It's important that you supply default values wherever possible.Besides using the
getParameter
method to get values of applet-specific parameters, you can also usegetParameter
to get the values of attributes of the applet's<APPLET>
tag. See The <APPLET> Tag for a list of<APPLET>
tag attributes.
AppletButton
Below is theAppletButton
code that gets the applet's parameters. For more information onAppletButton
, see the previous page.String windowClass; String buttonText; String windowTitle; int requestedWidth = 0; int requestedHeight = 0; . . . public void init() { windowClass = getParameter("WINDOWCLASS"); if (windowClass == null) { windowClass = "TestWindow"; } buttonText = getParameter("BUTTONTEXT"); if (buttonText == null) { buttonText = "Click here to bring up a " + windowClass; } windowTitle = getParameter("WINDOWTITLE"); if (windowTitle == null) { windowTitle = windowClass; } String windowWidthString = getParameter("WINDOWWIDTH"); if (windowWidthString != null) { try { requestedWidth = Integer.parseInt(windowWidthString); } catch (NumberFormatException e) { //Use default width. } } String windowHeightString = getParameter("WINDOWHEIGHT"); if (windowHeightString != null) { try { requestedHeight = Integer.parseInt(windowHeightString); } catch (NumberFormatException e) { //Use default height. } }
Start of Tutorial > Start of Trail > Start of Lesson | Search |