Passing JS function to applet for as event listener
-
02-07-2019 - |
Question
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);
Solution
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
OTHER TIPS
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
}
}
ps. to use JSObject you may need to include "MAYSCRIPT" tag to applet html tag.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow