javafx在java内吗,在JavaFX中使用JavaScript调用java。可能吗?

I am using WebEngine & WebView from JavaFX. Now I want to execute Java using javascript running inside WebEngine.

My question is if it is possible to do so and if yes any hints.

I want do something like below

function runSampleJava() {

var number = new java.lang.Integer(1234);

var random = new java.util.Random();

java.lang.System.out.println(random.nextInt());

}

Now if I call the runSampleJava() inside WebEngine it will execute that code.

Points to be noted

This is not about Rhino JavaScript engine for java

I know it is possible to inject java object, eg: JSObject window = (JSObject) webEngine.executeScript("window"); and so on. But this is not what I am looking for.

解决方案

I didn't manage to create Java instances but the thing I managed to do is to push object instances created in Java into JavaScript and call back to them.

So my Java-Code looks like this:

JSObject win = (JSObject) engine.executeScript("window");

win.setMember("jHelper", new JavaHelper());

JavaHelper example (must be public):

public static class JavaHelper {

public int newInteger(int input) {

// ...

}

public Random newRandom() {

// ...

}

}

And then then in JavaScript:

function bla() {

var number = jHelper.newInteger(1234);

var random = jHelper.newRandom();

// ...

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值