Advertisment

Script Talks to Applet

author-image
PCQ Bureau
New Update

Applet-script communication is the secret behind the search in our CDs. Entered Keywords are submitted to a Java Applet (Script-to-Applet communication). The applet reads the index file containing the articles and CD software descriptions and in turn communicates with JavaScript (Applet-to-Script communication) to search and display the results. 

Advertisment

First, we see how a JavaScript can invoke a method (function) in a Java Applet. Consider the following Web page:

























Advertisment

















Create a directory, say pcquest, and save the above file as example.htm.

The code for the Java Applet is:








import java.applet.*;


import java.awt.*;





public class JavaApplet extends Applet


{


private String message=”Hi from Java Applet”;


public void changeMessage( )


{


message=”Hi from JavaScript”;


repaint( );


}


public void paint(Graphics g)


{


g.drawString(message,10,10);


}


}















Advertisment

Save this file as JavaApplet.java in pcquest directory. With JDK installed, issue the following command to compile it.

javac JavaApplet.java 

Now open the page example.htm in IE or Navigator. Initially the applet will show the message “Hi from Java Applet”, but when you click on the button labeled “talk to Applet” the message changes to “Hi from JavaScript”. 

Advertisment

How did it happen?





In the HTML page, when the button is clicked, the JavaScript method talkToApplet( ) is called, as you can see, the onClick attribute is set to this method. 

The talkToApplet( ) method includes just one statement which is in the form:







document..







Here the name of the applet is the name given to it in the applet tag, which is MyApplet. The method of this applet that we wish to call is changeMessage( ). In the applet code you can see that this method sets the string variable message to “hello from JavaScript” and calls the repaint( ) method which paints or draws the new string on the applet. 

The magic word, which allows the JavaScript to communicate with the Applet, is MAYSCRIPT (included in the applet tag). Through this you declare explicitly that the applet can be accessed by a script. It must, however, be noted that only public methods and variables can be accessed. So, you can declare a private variable as public and directly change it though the script as :

Advertisment

document.MyApplet.message=”Hi from JavaScript”;



Passing parameters to Applet


If we modify the method changeMessage( ) in JavaApplet as:

public void changeMessage( String msg)



{


message=msg;


repaint( );


}


then we can pass any string (to be displayed) from the method talkToApplet( ) as:

Advertisment

document.MyApplet.changeMessage(“hello from Shekhar”);

Returns from Applet

The applet can also return values to JavaScript. Modify the JavaScript statement as:

Advertisment

var result = document.MyApplet. changeMessage(“hello from Shekhar”);

and add the following statement below it.

alert(result); 

That is, we will be store the value returned by the applet’s method in a JavaScript variable named result and then display it through an alert box. 

Modify the changeMessage( ) method in the applet as:

public String changeMessage (String msg)

{



message=msg;


repaint( );


return “Java Applet has changed the message”;


}


Note that we have specified that the method will now return a String (earlier it was retuning void or nothing). The method is returning a string “Java Applet has changed the message”, in its last statement. Remember to recompile the applet. 

When you click on the button on the page, the message displayed by the applet will change as well as an alert dialog box will pop up showing the message returned by the applet. 

You might have noticed that the Java String, “Java Applet has changed the message”, is automatically accepted as a JavaScript string when received by the script. In the script where we passed a JavaScript string “hello from shekhar” to the applet, it is automatically converted into Java String. So, JavaScript and Java Strings can be passed between applets and scripts seamlessly. Similar is with JavaScript and Java Boolean types. However, Javascript numeric types are converted into Java float and Java numeric data types are converted into JavaScript Number type.

Watch this space next month for how a Java applet can call JavaScript methods and other things that involve Applet-to-Script communication. 

Shekhar Govindarajan

Advertisment