Java Applet<-->JavaScript communication (Firefox 3).

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel Pitts

    Java Applet<-->JavaScript communication (Firefox 3).

    I only need to support Firefox 3 at the moment, so any advice can be
    specific to that.

    I have a Java Applet, lets call it HelperApplet.

    It has one method of importance:
    public Object doHelp(JSObject callback) {
    System.out.prin tln(callback + "\n" + callback.getMem ber("foo"));
    callback.call(" helped", new Object[] {callback.getMe mber("foo")});
    return callback;
    }

    I have javascript that does this:

    myCallback = documents.apple ts[0].doHelp({
    foo: "bar",
    helped: function(result ) {
    alert(result);
    }
    });

    Unfortunately, the callback doesn't get called. It appears the the
    JSObject callback doesn't contain any members in Java land. Although, I
    can access the returned object, eg myCallback.help ed("Broken Java") works.

    Any advice? Work-arounds? Are my attempts probably going to be futile?

    Thanks,
    Daniel.

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>
  • Daniel Pitts

    #2
    Re: Java Applet&lt;--&gt;JavaScri pt communication (Firefox 3).

    Daniel Pitts wrote:
    I only need to support Firefox 3 at the moment, so any advice can be
    specific to that.
    >
    I have a Java Applet, lets call it HelperApplet.
    >
    It has one method of importance:
    public Object doHelp(JSObject callback) {
    System.out.prin tln(callback + "\n" + callback.getMem ber("foo"));
    callback.call(" helped", new Object[] {callback.getMe mber("foo")});
    return callback;
    }
    >
    I have javascript that does this:
    >
    myCallback = documents.apple ts[0].doHelp({
    foo: "bar",
    helped: function(result ) {
    alert(result);
    }
    });
    >
    Unfortunately, the callback doesn't get called. It appears the the
    JSObject callback doesn't contain any members in Java land. Although, I
    can access the returned object, eg myCallback.help ed("Broken Java") works.
    >
    Any advice? Work-arounds? Are my attempts probably going to be futile?
    >
    Thanks,
    Daniel.
    >
    I figured out a work around, not pretty, but it does the trick:

    public class MyApplet extends JApplet {
    private JSObject appletTmp;

    public JSObject resolveObject(O bject o) {
    final int hashCode = System.identity HashCode(o);
    appletTmp.setMe mber("toResolve " + hashCode, o);
    return (JSObject) appletTmp.getMe mber("toResolve " + hashCode);
    }

    public void init() {
    if (appletTmp == null) {
    final JSObject window = JSObject.getWin dow(this);
    final String tmpName = "_AppletTmp " +
    System.identity HashCode(this);
    window.eval("va r "+ tmpName +" = {}");
    appletTmp = (JSObject)windo w.getMember(tmp Name);
    }
    }

    public Object doHelp(Object o) {
    JSObject callback = resolveObject(o );
    System.out.prin tln(callback + "\n" + callback.getMem ber("foo"));
    callback.call(" helped", new Object[] {callback.getMe mber("foo")});
    return callback;
    }
    }

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity .net/wordpress/>

    Comment

    Working...