Hello everybody.
Anybody knows why the following code is not working? Does it have anything to do with the b4j version?
I have version 10.00 Beta which is the latest as I think.
Thanks.
it works onky if I comment the following line which makes the code useless:
Anybody knows why the following code is not working? Does it have anything to do with the b4j version?
I have version 10.00 Beta which is the latest as I think.
Thanks.
B4X:
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private WebView1 As WebView
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
Dim html As String=$"<html>
<head>
<script>
var count = 0;
function doCallback() {
count++ ;
var msg ='Hello world '+count;
b4j.CallSub('show_msg', msg);
document.getElementById('test').innerHTML='test '+count;
console.log(count);
}
</script>
</head>
<body>
<div>
<button onclick='doCallback();'>Call Java</button>
</div>
<div id='test'></div>
</body>
</html>
"$
WebView1.LoadHtml(html)
Wait For WebView1_PageFinished
Dim we As JavaObject
Dim jo As JavaObject=WebView1
we = jo.RunMethod("getEngine",Null)
(Me).As(JavaObject).RunMethod("setBridge",Array(we))
LogError("Bridge set")
End Sub
Public Sub show_msg(msg As String)
Log(msg)
End Sub
Private Sub WebView1_PageFinished (Url As String)
Log("Finished page loading")
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
#If java
import javafx.scene.web.WebEngine;
import netscape.javascript.JSObject;
import java.lang.RuntimeException;
import java.lang.IllegalAccessException;
import java.lang.reflect.InvocationTargetException;
import javafx.application.Platform;
import java.lang.Enum;
import java.lang.reflect.Method;
public static class Bridge{
public static void CallSub(String sub,Object arg) {
System.out.println("CallSub called");
boolean isInt = false;
int ti = 0;
try{
Class<?> c = Class.forName("b4j.example.main");
Class<?> 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(){
try{
m.invoke(dummy,xarg);
}catch (IllegalAccessException e){
System.out.println(e);
}catch (InvocationTargetException ite){
System.out.println(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);
System.out.println("Bridge set: " + b + " member: 'b4j'");
}
#End If
B4X:
b4j.CallSub('show_msg', msg);