How can we pass parameters to applet from HTML files? Explain with proper example.
We can pass parameters into an applet via the <param name="paramName" value="param Value"/> tag nested within the <applet> tag. Inside the applet source code, we can use getParameter(StringparamName) to get the paramValue in the form og String; or null if the parameter does not exist. For example,
Example //.java File
import java.awt.*;
import javax.swing.*;
public class ParamTest extends JApplet
{
String msg;
public void init ()
{
msg= getParameter("message");
if (msg=null) msg = "Hi";
createGUI();
}
private void createGUI()
{
Container cp = getContentPane();
cp.setBackground (new Color (204, 238, 241));
cp.add(new JLabel(msg, JLabel.CENTER));
}
}
//.html File
<html>
<head>
<title>A Swing Applet with Parameter</title>
</head>
<body>
<h3>A Swing Applet with Parameter</h3>
<applet code="ParamTest.class" width="300" height="60" alt="Error Loading Applet!">
<param name="message" value="Hello, world!" />
</applet>
</body>
</html>
Comments
Post a Comment