Invoking Java from Javascript using BrowserComponent and not using Deprecated Classes

19 views
Skip to first unread message

x3.m...@gmail.com

unread,
Jan 22, 2018, 9:20:46 AM1/22/18
to CodenameOne Discussions
Hi guys,
I just love CN1 and am playing around with the Java/Javascript interoperability in the BrowserComponent.
The problem I have is that the working code has deprecated classes and I cannot find a good example of how to do it without using deprecated code.

Currently this works :
        JavascriptContext ctx = new JavascriptContext(browser);
        JSObject test = (JSObject) ctx.get("window");
        test.set("call", new JSFunction() {
            public void apply(JSObject self, Object[] args) {
                String msg = (String) args[0];
                System.out.println("window.call was executes");
            }
        });

Deprecated classes: JavascriptContext,JSObject
Please give me a hint on how to NOT use the deprecated Classes to call Java from Javascript.

Steve Hannah

unread,
Jan 22, 2018, 9:30:11 AM1/22/18
to codenameone...@googlegroups.com
Use BrowserComponent.addJSCallback() instead.  Some examples in the GoogleMaps lib

Yours would be something like

myBrowserComponent.addJSCallback("window.test=function(msg){callback.onSuccess(msg)}", msg -> {
    String msgStr = msg.stringValue();
    System.out.println(msgStr);
});

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/b6194ee7-411c-4b68-8cb4-b05cf50f1cbe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

x3.m...@gmail.com

unread,
Jan 22, 2018, 11:06:56 AM1/22/18
to CodenameOne Discussions
Perfect!
Steve, my hero
Reply all
Reply to author
Forward
0 new messages