Reputation: 5086
Is it possible to pass a function/callback from javascript to a java applet?
For example i have an applet with a button that when pressed it will call the passed js callback
function onCommand() {
alert('Button pressed from applet');
}
applet.onCommand(onCommand);
Upvotes: 4
Views: 2924
Reputation: 6697
ps. to use JSObject you may need to include "MAYSCRIPT" tag to applet html tag.
Upvotes: 3
Reputation: 171114
I tend to use something I derived from the reflection example at the bottom of this page, as then you don't need to meddle with your classpath to get it to compile
Then I just pass JSON strings around between the applet and javascript
Upvotes: 4
Reputation: 89085
You can use JSObject to call back into javascript from Java.
From that page:
import netscape.javascript.*;
import java.applet.*;
import java.awt.*;
class MyApplet extends Applet {
public void init() {
JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject loc = (JSObject) doc.getMember("location");
String s = (String) loc.getMember("href"); // document.location.href
win.call("f", null); // Call f() in HTML page
}
}
Upvotes: 3