Getting Java Applets to talk to JavaScript

Jake Howlett, 14 March 2001

Category: Java; Keywords: applet javascript java LiveConnect

Not so long ago I got a couple of e-mails. One asked if I knew how to control an Applet using JavaScript and the other asked me if I knew how to call JavaScript routines from an Applet. At the time I didn't have a clue. However, not being one to turn down a challenge I managed to solve the first problem and this document describes how I solved the second.

Having little experience of Applets I had to ask myself: "CAN it be done?". Well, having looked behind the scenes of the Action Buttons in the R5 View Applets, I discovered they do nothing more than call JavaScript function similar to their HTML counterparts, I knew it could be done. So I set off to find out how...

By way of example here is an Applet that lets you select a colour or add a new one and then, using the "Add" button, pass the value to a JavaScript function in the document. This function then appends the value to the <textarea> called "Choices".



You have chosen all of these:


This particular example is by no means rocket-science and, at the end of the day, is next to useless. However, just imagine the possibilities of combining Java AND JavaScript on your page. One thing that jumps to mind (well mine anyway) is that the Applet could use CORBA to connect to the server. This means we could fetch data from the server without having to reload the page. Nice! No more Keyword Refresh nightmares.

So how do we go about writing an Applet like this. Well I'm not about to start talking about how to write an Applet, just the bits relevant to this example. The technology that is used was created by Netscape (wow, they have done something worthy of praise) and is called LiveConnect. This is the same technology used when JavaScript talks TO the Applet.

The first thing to do is make sure that the necessary classes are imported to the applet using the line below:

 import netscape.javascript.*;
Now we can use this in the code-block below:
public void doAdd() {
//Get a handle on the DOM Window Object of the browser;
JSObject win = JSObject.getWindow(this);

//Work out what to send.
String add = null;
String choice = getdpdChoose().getSelectedItem();
String addnew = gettxtAddNew().getText();

if ( !choice.equals( getParameter( "DefaultChoice" ) ) ){
add = choice;
} else if ( !addnew.equals("") ) {
add = addnew;
} else {
add = "";
}
//Send the value to the JavaScript function in the browser
win.eval("addToList(\'" + add + "\')");
}
The first thing we do here is create a JSObject object that represents the browser window object. The method then looks at the drop-down list and the text field on the applet and works out what the user wants to send to the browser. This is then passed to the browser window, via JavaScript, using the eval method.

The only other thing we need to do is create a function in the browser to process this call. In this case it is a function called "addToList" that simply adds the valus to the end of a field, like below:
function addToList( val ){
if ( val == "" ) return alert( 'You haven\'t selected
anything in the applet.' );
fld = document.forms[0].Choices;
fld.value += (fld.value=="") ? val : '\n' + val;
}
As I mentioned earlier this methodolgy is VERY powerful. There are almost endless number of applications. If you come up with anything interesting, don't forget to share it with us all...

Other Reading:
Sun's Documentation on using Java and JavaScript.
Netscape's Documentation on using Java and JavaScript.



Notes: The Applet has amongst its attributes - "MAYSCRIPT". This is required (security issue) to be set before this kind of communication can occur.