B4J Question Making Upcalls from JavaScript to JavaFX?

Discussion in 'B4J Questions' started by zyblux, Mar 13, 2015.

  1. zyblux

    zyblux Member Licensed User

    Hi Everyone,
    I have recently been doing web development using B4j and found it extremely nice to use. I have no problems handling JS code from the server side and invoking JS from a desktop app using the WebView control (and JavaObject etc). But is it possible to invoke B4j code from JS running in the webview control? I found an example in the Oracle blog but not sure how to get it to work in B4j:

    Any ideas?



    Making Upcalls from JavaScript to JavaFX

    Since we are talking about a two-way communication channel, what about making calls in the opposite direction: from a Web application into JavaFX? On the JavaFX side, you need to create an interface object (of any class) and make it known to JavaScript by calling JSObject.setMember(). Having performed this, you can call public methods from JavaScript and access public fields of that object.

    The code below shows how to set up an interface object:

    class Bridge {
    public void exit() {
    JSObject jsobj = (JSObject) webEngine.executeScript("window");
    jsobj.setMember("java", new Bridge());

    First we need a JSObject to attach our interface object to. The above code uses the JavaScript window object but any other object would work as well. Note that a cast is necessary. Then we create an interface object and add it as a new member of that JSObject. It becomes known to JavaScript under the name window.java, or just java, and its only method can be called from JavaScript as java.exit(). The upcall into Java is synchronous and occurs on the JavaFX Application thread. The following HTML code enables exiting the JavaFX application by clicking on a link:

    <a href="" onclick="java.exit();">here</a>
    to exit the application

    Once you no longer need an interface object, you may want to call the JSObject.removeMember() method to make JavaScript "forget" it.
  2. Erel

    Erel Administrator Staff Member Licensed User

    Which type of server are you implementing? WebSockets?
  3. zyblux

    zyblux Member Licensed User

    Hi Erel,
    Yep, standard web sockets but I'm using the a webview control in a desktop app but I can try anything :)

  4. Erel

    Erel Administrator Staff Member Licensed User

  5. zyblux

    zyblux Member Licensed User

    Sorry Erel, I don't think I explained myself very well. I'm trying to do a desktop/web client hybrid kind of thing.

    I have a standard desktop app with a webview control on the form. I have no problem with websockets and jQuery etc from the server side - that whole mechanism is awesome. BUT what I'm trying to do is for certain web links to trigger code on the client side "outside" of the webview control. So clicking on a <button> tag in a web page will trigger an event in the deskop UI code not the ws/server, breaking out of the whole websocket stuff. I found examples of triggering JS in the webview control from UI code but I'd like to get it working both directions.

  6. Erel

    Erel Administrator Staff Member Licensed User

  7. scrat

    scrat Active Member Licensed User


    I have a similar problem.
    I try to use leaflet.js with b4j and i must call sub from JS to B4j and from B4j to JS.

    With B4a I use webviewextras and all works fine.

    With b4j call JS function are ok but the problem is to call B4j function from JS.

    I use this code.
    Dim win As JavaObject
    "getEngine",Null).RunMethodJO("executeScript"Array As String("window"))
    "setMember",Array As Object("b4j",MainForm))

    sub test
    log("from js")
    end sub
    function userdragend() {
    Java code from Oracle

    public class JavaApplication {
    public void exit() {
    JSObject window = (JSObject) webEngine.executeScript("window");
    window.setMember("app", new JavaApplication());
    No error but test sub is never called.
    How to pass B4J class to the webengine ?

    Thank you
  8. Daestrum

    Daestrum Well-Known Member Licensed User

    This example should get you started.
    Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim wv As WebView
    End Sub

    Sub AppStart (Form1 As Form, Args() As String)
      MainForm = Form1
    'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
    Dim we As JavaObject
      we = asJO(wv).RunMethod(
    <a href="" onclick="b4j.test();">here</a>
    to exit the application"$
    End Sub

    Sub asJO(o As JavaObjectAs JavaObject
    Return o
    End Sub

    Sub test()
    End Sub

    #if java

    import netscape.javascript.JSObject;
    import javafx.scene.web.WebEngine;
    import java.lang.RuntimeException;

    public static class Bridge{
       public void test() {
                 b4j.example.main._test();// redirect to b4j sub
                } catch (Exception e){

    public static void setBridge(String s,WebEngine we){
       JSObject jsobj = (JSObject) we.executeScript("window");
       jsobj.setMember(s, new Bridge());
    #end if
    amykonio likes this.
  9. Daestrum

    Daestrum Well-Known Member Licensed User

    A better example attached.
    This allows for calling the b4j sub by name instead of hard coding it.
    The link routine takes just the name of the sub no params
    The link2 routine takes the name plus a single parameter.
    The routines in b4j are run on a thread (runlater...)

    Note: link2 routine has some odd code as the javascript supplied an Integer and b4j wanted an int.

    Attached Files:

  10. scrat

    scrat Active Member Licensed User

    Thank you Daestrum, I 'll test this solution
  11. Eme Fibonacci

    Eme Fibonacci Well-Known Member Licensed User

    I am using the example above.
    Java inline:
      public void CallSub2(String sub,Object arg) { // for call with  1 param
            boolean isInt = false;
            int ti = 0;
                Class<?= Class.forName("b4j.example.main");
    ?> ac = arg.getClass();
                if (arg instanceof java.lang.Integer){
                    ac = int.class;
                final Method m = c.getDeclaredMethod("_" + sub.toLowerCase(),ac);
                final Object dummy = null;
                final Object xarg = arg;
                Platform.runLater(new Runnable(){
                    public void run(){
                        }catch (IllegalAccessException e){
                        }catch (InvocationTargetException ite){
            } catch (Exception e){
    In my javasctipt :
    function myFunction() {
    'Subname','Hello b4j');
    In B4J this works
    Sub Subname(s As String)
    End Sub
    But This Don't work. Why?
    Sub Subname(s As Object)
    End Sub
  12. DonManfred

    DonManfred Expert Licensed User

    1. you should have started a new thread for your issue.
    2. A javascript can not send Objects like in java. the parameter from your javascript is a string.
    techknight likes this.
  13. Daestrum

    Daestrum Well-Known Member Licensed User

    Class<?> ac = arg.getClass();
    Class<?> ac = Object.class;
    Then it will call the sub with an Object parameter is Sub fred(o As Object)
    DonManfred likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice