Chapter: Java The Complete Reference - The Java Library - The Applet Class

| Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail |

Passing Parameters to Applets

As just discussed, the APPLET tag allows you to pass parameters to your applet. To retrieve a parameter, use the getParameter( ) method.

Passing Parameters to Applets

 

As just discussed, the APPLET tag allows you to pass parameters to your applet. To retrieve a parameter, use the getParameter( ) method. It returns the value of the specified parameter in the form of a String object. Thus, for numeric and boolean values, you will need to convert their string representations into their internal formats. Here is an example that demonstrates passing parameters:

 

 

// Use Parameters

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

 

<applet code="ParamDemo" width=300 height=80> <param name=fontName value=Courier>

 

<param name=fontSize value=14> <param name=leading value=2>

 

<param name=accountEnabled value=true> </applet>

 

*/

 

public class ParamDemo extends Applet { String fontName;

int fontSize; float leading; boolean active;

 

// Initialize the string to be displayed.

public void start() {

 

String param;

 

fontName = getParameter("fontName"); if(fontName == null)

 

fontName = "Not Found";

 

param = getParameter("fontSize"); try {

 

if(param != null)

 

fontSize = Integer.parseInt(param); else

 

fontSize = 0;

 

} catch(NumberFormatException e) { fontSize = -1;

 

}

 

param = getParameter("leading"); try {

 

if(param != null)

 

leading = Float.valueOf(param).floatValue(); else

 

leading = 0;

 

} catch(NumberFormatException e) { leading = -1;

 

}

 

param = getParameter("accountEnabled"); if(param != null)

 

active = Boolean.valueOf(param).booleanValue();

 

}

 

// Display parameters.

 

public void paint(Graphics g) {

g.drawString("Font name: " + fontName, 0, 10);

g.drawString("Font size: " + fontSize, 0, 26);

g.drawString("Leading: " + leading, 0, 42);

g.drawString("Account Active: " + active, 0, 58);

}

 

}


Sample output from this program is shown here:

 

As the program shows, you should test the return values from getParameter( ). If a parameter isn’t available, getParameter( ) will return null. Also, conversions to numeric types must be attempted in a try statement that catches NumberFormatException. Uncaught exceptions should never occur within an applet.

Improving the Banner Applet

It is possible to use a parameter to enhance the banner applet shown earlier. In the previous version, the message being scrolled was hard-coded into the applet. However, passing the message as a parameter allows the banner applet to display a different message each time it is executed. This improved version is shown here. Notice that the APPLET tag at the top of the file now specifies a parameter called message that is linked to a quoted string.

 

// A parameterized banner

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

/*

 

<applet code="ParamBanner" width=300 height=50> <param name=message value="Java makes the Web move!"> </applet>

 

*/

 

public class ParamBanner extends Applet implements Runnable { String msg;

 

Thread t = null; int state;

 

volatile boolean stopFlag;

 

    //Set colors and initialize thread.

    public void init() {

 

setBackground(Color.cyan);

 

setForeground(Color.red);

 

}

 

    //Start thread

 

public void start() {

 

msg = getParameter("message");

 

if(msg == null) msg = "Message not found."; msg = " " + msg;

 

t = new Thread(this); stopFlag = false; t.start();

 

}

 

// Entry point for the thread that runs the banner.

public void run() {

 

// Redisplay banner

for( ; ; ) {

 

try { repaint();

 

Thread.sleep(250);

 

if(stopFlag)

 

break;

 

} catch(InterruptedException e) {}

 

}

 

}

    Pause the banner. public void stop() {

 

stopFlag = true; t = null;

 

}

 

    //Display the banner.

 

public void paint(Graphics g) { char ch;

 

ch = msg.charAt(0);

 

msg = msg.substring(1, msg.length()); msg += ch;

 

g.drawString(msg, 50, 30);

 

}

 

}

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail


Copyright © 2018-2020 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.