B4J Question [SOLVED]From WebView to b4j B4XPages ?

Duque

Active Member
Licensed User
Longtime User
main_creditos It's a page

B4X:
#if java
import netscape.javascript.JSObject;
import javafx.scene.web.WebEngine;
import java.lang.RuntimeException;
import java.lang.IllegalAccessException;
import java.lang.reflect.InvocationTargetException;

import java.lang.Enum;
import java.lang.reflect.Method;
import javafx.application.Platform;


public static class Bridge{
    public void link(String sub) {
        try{
            Class<?> c = Class.forName("b4j.analit2.main_creditos");
            final Method m = c.getDeclaredMethod("_" + sub.toLowerCase());
            final Object dummy = null;
            Platform.runLater(new Runnable(){
                public void run(){
                    try{
                        m.invoke(dummy);
                    }catch (IllegalAccessException e){
                        System.out.println(e);
                    }catch (InvocationTargetException ite){
                        System.out.println(ite);
                    }
                }
            });
        } catch (Exception e){
            System.out.println(e);
        }
    }
    public void link2(String sub,Object arg) {   // for call with  1 param
        boolean isInt = false;
        int ti = 0;
        try{
            Class<?> c = Class.forName("b4j.analit2.main_creditos");
            Class<?> ac = arg.getClass();
            /*
                    Sub Subname(s As String)
                        Log(s)
                    End Sub
              forum change line: Then it will call the sub with an Object parameter is Sub subname(ob As Object)
                Class<?> ac = Object.class;
                    Sub Subname(ob As Object)
                        Log(ob)
                    End Sub
            */
            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(){
                    try{
                        m.invoke(dummy,xarg);
                    }catch (IllegalAccessException e){
                        System.out.println(e);
                    }catch (InvocationTargetException ite){
                    }
                }
            });
        } catch (Exception e){
            System.out.println(e);
        }
    }

}
public static void setBridge(WebEngine we){
    JSObject jsobj = (JSObject) we.executeScript("window");
    Bridge b = new Bridge();
    jsobj.setMember("b4j", b);
}
#end if
Sub consume_event_web(str As String)
    Log(str)
End Sub
 
Upvote 0
Top