Monday, March 28, 2011

How to access objects/arrays defined in JavaScript from Java applets.

Using LiveConnect getMember(String) method of a window JSObject, a Java object of type "Object" is returned but I don't find the expected members defined on it. Neither can I cast it to a JSObject to continue using the getMember().

Accessing DOM nodes and elements (DOM Objects) works as expected.

From stackoverflow
  • @Michael Borgwardt

    This is what I'm trying to do.

    Global JavaScript Object.

    foo = {"one":1, "two":2};
    

    Processing code.

    import netscape.javascript.*;
    JSObject win;
    JSObject got1;
    Object   got2;
    
    void setup(){
      size(400,200,P2D);
      background(255);
      win  = JSObject.getWindow(this); //gets the root JSObject
      //got1 = win.getMember("obj"); //fails to cast from Object to JSObject
      got2 = win.getMember("obj"); //works in the sense that I get an Object.
    }
    
    void draw(){
      if (mousePressed == true){
        println(got2.getMember("one")); //fails, there is no getMember() method.
      }
    }
    

    This is perhaps not Processing specific. That's why I didn't originally post the code.

  • I'm confused by the line got2 = win.getMember("obj");. It seems like you're expecting that line to get the foo object. As I understand it, win.getMember("obj") will get you the obj property on the DOM window node, which doesn't exist. For example, from this page:

    JSObject win = JSObject.getWindow(this);
    JSObject doc = (JSObject) win.getMember("document");
    JSObject loc = (JSObject) doc.getMember("location");
    

    If you want to get the foo object, the best way I'm aware of is to make a JavaScript method you can call from the Java applet.

    JavaScript:

    <script type="text/javascript">
    var foo = {"one":1, "two":2};
    
    function getFoo() { return foo; }
    </script>
    

    Applet:

    import netscape.javascript.*;
    JSObject win;
    JSObject foo;
    
    void setup(){
      size(400,200,P2D);
      background(255);
      win  = JSObject.getWindow(this); //gets the root JSObject
      foo = (JSObject)win.call("getFoo", null);
    }
    
    void draw(){
      if (mousePressed == true){
        println(foo.getMember("one"));
      }
    }
    
    Matthew Maravillas : Yes, but I don't get why you expect it to get the "foo" object. "JavaScript object" just means it's a object from JavaScript that's been translated to a Java object, specifically JSObject.
    Matthew Maravillas : It did. What's your exception?
    Matthew Maravillas : Random guess: try clearing your classloader cache? Bring up your Java console (Tools - Java console in FF, or right click on the Java icon in your systray and pick the console), and hit x to clear it, then reload.
    Matthew Maravillas : I'm not sure I have any more suggestions for you, except to be pedantic - are you using the exact code I wrote above? I can get the exception if I have getFoo return null, but my line number is 32 instead of 34, so I suspect you have something else there as well.

0 comments:

Post a Comment