Getting Java Applets to talk to JavaScript

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.


  1. package netscape.java not found in import


    first of all I'd like to say that this site is great and ful of usefull information !

    When i tried the code in an agent I get following message: "package netscape.java not found in import" and I can't compile.

    Where can I obtain this package?

    (using R5 on W2000 SP2, IE5.5)

    Thanks in advance!

      • avatar
      • Andrew Junkuhn
      • Thu 25 Apr 2002

      Re: package netscape.java not found in import

      Go to C:\WINNT\java\Packages and you will find a bunch of zip files. In one of them you will find 2 class files called: JSException.class JSObject.class

      Add these two files to your class path.

      Show the rest of this thread

  2. Date Picker created in Java Scrip

    Dear Sir

    Can you please tell me how do i interate the date picker which i have developed in to java Script in to Java Applet which is of Awt library.

    Please reply

  3. Does this works for all browsers?

    such as IE, firefox, chrome, etc...

Your Comments


Navigate other articles in the category "Java"

« Previous Article Next Article »
Recreate LotusScript Print method in Java   Java Date-Picker Applet

About This Article

Author: Jake Howlett
Category: Java
Keywords: applet; javascript; java; LiveConnect;


J2JS.class (5 Kbytes)
J2JS.java (5 Kbytes)


Print Friendly

Let's Get Social

About This Website

CodeStore is all about web development. Concentrating on Lotus Domino, ASP.NET, Flex, SharePoint and all things internet.

Your host is Jake Howlett who runs his own web development company called Rockall Design and is always on the lookout for new and interesting work to do.

You can find me on Twitter and on Linked In.

Read more about this site »