Calling Javascript from Java using LiveConnect

I've been struggling with this all day. I want to call a JavaScript function from a java applet, using code that will work in IE4 and Nav 4. LiveConnect should do this. I've followed examples on netscape web site. The following java code below works on IE4 but not on Nav 4. Why?

import netscape.javascript.JSObject;
public class Example3 extends Applet {
   JSObject win;

       * Initialisation section.
      public void init() {

        // Get a reference to the Navigator Window
            win = JSObject.getWindow(this);
        catch (Exception e){

            Button b = new Button("Say Hello");
            b.addActionListener(new ActionListener() {
                  public void actionPerformed(ActionEvent e) {
                        System.out.println("in button click");

   public void sayHello() {
        win.eval("alert(\"Hello World\");");

      public void paint(Graphics g) {
            g.drawString("Click the button to say hello", 100, 100);
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ncadgeAuthor Commented:

I've tried re-installing Nav.

I have C:\Program Files\Netscape\Communicator\Program\Java\Classes\java40.jar in my CLASSPATH.

You can do it with LiveConnect, although I've only tested this with Communicator 4.7 and IE5. It seems to work with both.

Compile and run the applet to see what it does.. It seems to communicate between Javascript and Java.

The java code:

 * Created on 8 December 1999, 13:59
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.awt.event.*;
import netscape.javascript.*;

 * @author  WilsonT
 * @version
public class SubHeadingsApplet extends Applet {
    private Choice subHeadingsChoice;

     * add a string to the choice box
     * @param subHeadings - the sub heading to add
    public void addSubHeading (String subHeading) {
          subHeadingsChoice.addItem (subHeading);
    } // end method

     * get the selected sub heading
     * @return - the selected sub heading
    public String getSelectedSubHeading () {
          return subHeadingsChoice.getSelectedItem ();
    } // end method
    public void init () {
            initComponents ();
            readParameters ();

    private void initComponents () {
        subHeadingsChoice = new Choice ();
        setLayout (new GridLayout (1, 1));
        setBackground (Color.white);
        setForeground (;

        subHeadingsChoice.addItemListener (new java.awt.event.ItemListener () {
            public void itemStateChanged (java.awt.event.ItemEvent evt) {
                headingSelected (evt);
            } // end method
        } // end class

        add (subHeadingsChoice);
    } // end method

      private void headingSelected (java.awt.event.ItemEvent evt) {
            try {
                  JSObject jsObject = (JSObject) JSObject.getWindow (this);
              Object[] objectArray = new Object[1];
              objectArray[0] = subHeadingsChoice.getSelectedItem ();
     ("headingSelected", objectArray);
            } // end try
            catch (JSException jse) {
            } // end catch
            catch (Exception e) {
            } // end catch    
      } // end method

     * Read the parameters from the HTML file containing the applet
    private void readParameters () {
        // get all the parameters for the choice box
        String parameter = this.getParameter ("Heading1");

          int x = 2;
          while (parameter != null) {
                  this.addSubHeading (parameter);

                parameter = this.getParameter ("Heading" + x);
          } // end while
    } // end method

     * remove all the headings from the choice box
    public void removeSubHeadings () {
          subHeadingsChoice.removeAll ();
    } // end method
} // end class

Now the HTML code:


            <APPLET NAME="SubHeadingsApplet" CODE="SubHeadingsApplet.class" WIDTH="200" HEIGHT="50" MAYSCRIPT>
                  <PARAM NAME="Heading1" VALUE="Fred">
                  <PARAM NAME="Heading2" VALUE="Bill">

                  <INPUT NAME="textfield" TYPE="text">
                  <INPUT TYPE="button" VALUE="Add" onClick="addHeading(textfield.value)">
                  <INPUT TYPE="button" VALUE="Clear" onClick="clearHeadings()">

      function addHeading(heading) {
      } // end function

      function clearHeadings() {
      } // end functions

      function headingSelected(heading) {
            alert (heading);
      } // end function


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.