by June 7, 2002 0 comments

In our article Script Talks to Applet (page 96, PCQuest, May 2002), we talked about how JavaScript can interact with a Java applet. In this article
we’ll look at the other side–applet to script communication.

In our first example you’ll see how a Java applet can call a JavaScript method, defined within the <script> </script> tag. Jot the following in a notepad and save the file as example1.htm in a directory (pcquest in our case).

<script language=”JavaScript”>
var message=”Hi from JavaScript”;
function changeMessage( )
var message=”Hi from Java”;
<form name=”form1”>
<input name=”messagefield” type=”text” size=”30” value=”Hi from JavaScript”>
<applet code=”JavaApplet1” name=”MyApplet” width=”120” height=”50”

Here the changeMessage( ) method (function), sets the value of the textfield (named messagefield) to “Hi from Java”. The attribute MAYSCRIPT in the <applet> tag declares that an applet can access a script. Next, write the Java applet that will call the method
changeMessage( ).

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;
public class JavaApplet1 extends Applet implements ActionListener

public void init()
documentWindow = JSObject.getWindow(this);
catch(Exception e)
System.out.println(“Error: “+e);
button = new Button(“Talk to Script”);
public void actionPerformed(ActionEvent e)
String[] args = {“”};“changeMessage”,args);
private JSObject documentWindow;
private Button button;

Save this file as in the pcquest directory.

Classes in IE and Netscape
Two classes, JSObject and JSException (for handling errors/exceptions), let an applet communicate with JavaScript. These classes are in a package named netscape.javascript (though this package is not included in the standard JDK distribution). IE and Netscape Navigator, however, come with these classes. Different versions of these browsers have different names for the archive file containing this package.

In the case of IE, a zip file containing this package is in the directory java/packages in the windows or winnt directory. The file may be named like,, or and may vary across versions of IE.

In the case of Netscape Navigator 4.x, the package is archived as a jar file named java40.jar and can be found in the directory program

To verify that the zip or the jar file contains the required package, open the archive file using WinZip and look for the files named JSObject.class and JSException.class in the file listing.

One you have located the correct archive file, copy it to the directory pcquest. Next, compile the applet as

javac -classpath <archive-file-name>

For example,

javac -classpath
javac -classpath java40.jar

Open the file example1.htm in IE or Netscape. The text field initially shows “Hi from JavaScript”, but when you click on the Java applet button (labeled “talk to Script”), the text changes to “Hi from Java”.

How it happens
In the applet’s code, besides importing standard Java classes for displaying the button, event handling etc, you also import the classes in the package netscape.javascript as

import netscape.javascript.*;

The next step is to obtain a reference to the HTML page on which the JavaScript method to be invoked resides. This is done by the following statement.

documentWindow = JSObject.getWindow(this);

The documentWindow is an object of the class JSObject. Henceforth the reference to the HTML page is stored in the variable documentWindow. Note that this statement is wrapped within a try-catch block. If you compile the applet using the archive file from IE, it is mandatory to catch the exception (JSException) at this place. However, in you use Netscape’s archive file, you don’t need the try-catch blocks.

A JSException is thrown when an error occurs during applet-to-script communication. For example, if the attribute MAYSCRIPT is not included within the applet tag in the HTML page, then a JSException describing the error is thrown.

The main action takes place in the actionPerformed( ) method, which is executed when the button on the applet is clicked. A method named call( ) of the JSObject class can be used to invoke any JavaScript method on the HTML page.

The call( ) method has two parameters or arguments. The first is the name of the JavaScript method to be called and the second is a string array of arguments that the JavaScript method requires.

In the actionPerformed( ) method, the statement“changeMessage”,args);

calls changeMessage( ). Since the method changeMessage( ) does not have any arguments, we declare an empty String array named args as

String[] args = {“”};

and pass it as the second argument for the call( ) method.

Passing parameters to script
We can also call built-in JavaScript functions, like alert( ) and prompt( ), directly from the applet. Modify the method actionPerformed( ) as follows.

public void actionPerformed(ActionEvent e)

String[] args = {“Hi from Java”};“alert”,args);

Recompile the applet and open example1.htm in a Web browser. This time when you click on the applet’s button, a JavaScript alert box will be displayed with the message “Hi from Java”. Note that here we have demonstrated the passing of a parameter (the message to be displayed) to the alert( ) JavaScript method. Similarly, you can pass parameters used defined JavaScript methods like changeMessage( ) after modifying it to accept the parameters as

function changeMessage(msg)

Returns from Script
Modify the actionPerformed( ) method as follows.

public void actionPerformed (ActionEvent e)

String[] args = {“Enter a new label for the Applet’s button”,”Talk to Script”};
String label = (String)“prompt”,args);

The prompt( ) method of JavaScript displays a dialog box with some text and a textfield where the user can enter something. The textfield can be pre-filled with some default value. In this case, we want to display a dialog box with the text, (Enter a new label for the Applet’s button) and a textfield with a default value (talk to script). So we pass these two string parameters to the prompt( ) method.

After recompiling, open example1.htm in the browser. This time when you click on the applet’s button, a JavaScript prompt box will ask you for a new label for the applet’s button. Whatever you enter in the text field of the dialog box, becomes the return value of the prompt( ) method. We store this value in a Java String variable named label, as

String label = (String)“prompt”,args);

Note that the value returned is in the form of a generic Java Object. Hence we must cast it to String using the (String) prefix. Finally, the label of the button is changed by using its setLabel( ) method.

Shekhar Govindarajan

No Comments so far

Jump into a conversation

No Comments Yet!

You can be the one to start a conversation.