I'm cutting my teeth on some Java/JavaScript coding and I seem to have hit a wall. I'm trying to pass agruments from Java to JavaScript but no matter what I do "JSObject jso = JSObject.getWindow(this);" always throws an exception. I've done some searching and can't find any solutions. I stole the code below from a website (http://www.codejava.net/java-se/applet/liveconnect-the-api-for-communication-between-java-applet-and-javascript) and don't see any errors in either the JavaScript or the Java and both files compile correctly.
I've added plugin.jar to by buildpath and made sure that the jfxrt.jar is not in the build path. I thought something could possibly be wrong with the plugin.jar in jre7 so I tried jre6 but was getting the same error. The code I'm using is as follows.
Java Code:
package test;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import netscape.javascript.*;
public class TestApplet extends JApplet {
private JButton button = new JButton("Call Javascript");
private JLabel label = new JLabel();
public void init() {
getContentPane().setLayout(new BorderLayout());
getContentPane().add(button, BorderLayout.NORTH);
getContentPane().add(label, BorderLayout.SOUTH);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
Thread runner = new Thread(new Runnable() {
public void run() {
try {
testLiveConnect();
} catch (JSException jse) {
// Error
jse.printStackTrace();
}
}
});
runner.start();
}
});
}
private void testLiveConnect() throws JSException {
JSObject jso = JSObject.getWindow(this);
// call Javascript's method foo() with no argument
String result = (String) jso.call("foo", null);
label.setText(result);
// delay 2 seconds to see the result
try { Thread.sleep(2000); } catch (InterruptedException ie) {};
// call Javascript's method foo() with two arguments
result = (String) jso.call("bar", new String[] {"Alice", "Alisa"});
label.setText(result);
try { Thread.sleep(2000); } catch (InterruptedException ie) {};
// execute a Javascript expression
String expression = "alert('Hi, I am from Javascript.');";
jso.eval(expression);
try { Thread.sleep(2000); } catch (InterruptedException ie) {};
// get value of a named member from Javascript
result = (String) jso.getMember("coop");
label.setText(result);
try { Thread.sleep(2000); } catch (InterruptedException ie) {};
// get value of an indexed member from Javascript
result = (String) jso.getSlot(1);
label.setText(result);
}
}
JavaScript Code:
<html>
<head>
<title>LiveConnect - Java-Javascript communnication demo</title>
</head>
<body>
<center>
<applet id="testApplet"
code="TestApplet.class"
width="200" height="80"
>
</applet>
</center>
</body>
<script type="text/javascript">
var coop = "Ooops!";
this[1] = "Slot 1";
function foo() {
return "This is from foo()";
}
function bar(firstName, lastName) {
return "Greeting " + firstName + " " + lastName + "!";
}
</script>
</html>
Exception Thrown:
netscape.javascript.JSException
at netscape.javascript.JSObject.getWindow(Unknown Source)
at test.TestApplet.testLiveConnect(TestApplet.java:34)
at test.TestApplet.access$0(TestApplet.java:33)
at test.TestApplet$1$1.run(TestApplet.java:22)
at java.lang.Thread.run(Unknown Source)