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
        try{
            win = JSObject.getWindow(this);
        }
        catch (Exception e){
                   setBackground(Color.red);
        }        

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

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

      public void paint(Graphics g) {
            g.drawString("Click the button to say hello", 100, 100);
      }
}
ncadgeAsked:
Who is Participating?
 
TomWilsonConnect With a Mentor Commented:
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:

/*
 * SubHeadingsApplet.java
 *
 * 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 (Color.black);

        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 ();
                
              jsObject.call ("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);
                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:

<HTML>
    <HEAD>
    </HEAD>

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

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

<SCRIPT>
      function addHeading(heading) {
            document.applets["SubHeadingsApplet"].addSubHeading(heading);
      } // end function

      function clearHeadings() {
            document.applets["SubHeadingsApplet"].removeSubHeadings();
      } // end functions

      function headingSelected(heading) {
            alert (heading);
      } // end function
</SCRIPT>

0
 
ncadgeAuthor Commented:
ps.

I've tried re-installing Nav.

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

0
All Courses

From novice to tech pro — start learning today.