当使用javaFx从v6迁移到v.7时,jxBrowser出现异常。

问题描述 投票:0回答:1

我们正在将我们的应用程序从jxbrowser v6.14迁移到v7.1。在将所有的jxbrowser apis替换为新的apis后,我们将引用 v6-v7 doc我没有得到任何编译错误。然而,它陷入了一个IllegalStateException。由于这是在内部发生的,我无法找出问题所在。

import com.teamdev.jxbrowser.browser.Browser;
import com.teamdev.jxbrowser.browser.callback.AlertCallback;
import com.teamdev.jxbrowser.browser.callback.InjectJsCallback;
import com.teamdev.jxbrowser.browser.event.ConsoleMessageReceived;
import com.teamdev.jxbrowser.engine.Engine;
import com.teamdev.jxbrowser.engine.EngineOptions;
import com.teamdev.jxbrowser.js.JsObject;
import com.teamdev.jxbrowser.view.javafx.BrowserView;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.embed.swt.FXCanvas;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.stage.Stage;

import java.nio.file.Paths;


import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.*;

import static com.teamdev.jxbrowser.engine.RenderingMode.HARDWARE_ACCELERATED;


/**...*/
public final class HelloWorld extends Application {

  private Browser browser;
  private BrowserView view;
  protected Control control;
  private static Engine engine;

  public static void main(String[] args) {
    launch(args);
  }

  @Override
  public void start(Stage primaryStage) {

    Platform.setImplicitExit(false);
    browser = createBrowserObject();// creates the browser object

    Display display = new Display();
    Composite shell = new Shell(display);
    shell.setSize(400, 400);
    shell.setLayout(new FillLayout());

    final FXCanvas canvas = new FXCanvas(shell, SWT.NONE);

    view = BrowserView.newInstance(browser);
    Scene scene = new Scene(new BorderPane(view), 700, 500);
    canvas.setScene(scene);


    browser.on(ConsoleMessageReceived.class, evt -> {
//      Logger.log("Message: " + evt.consoleMessage().message());
    });

    browser.set(AlertCallback.class, (params, tell) -> {
//      Logger.log("Alert dialog shown to the user. Info: " + params.message());
      runOnUIThread(new Runnable() {

        @Override
        public void run() {
          String title = "Information";
          String message = params.message();
        }
      }, true);
      // send OK signal to JxBrowser so that it can proceed further, else it will remain blocked
      tell.ok();
    });


    // Inject bridge objects into JavaScript memory
    browser.set(InjectJsCallback.class, params -> {
      JsObject window = params.frame().executeJavaScript("window");
      Object bridge = null;//getBridge();
      if(bridge != null ){
        window.putProperty("console", bridge);
        window.putProperty("strategyBridge", bridge);
      }
      runOnUIThread(new Runnable() {

        public void run() {

          control.setData("initializing", "true");
          if(true){
            control.setFocus();
          }
          control.setData("initializing", "false");
        }
      }, false);

      return InjectJsCallback.Response.proceed();
    });


    browser.navigation().loadUrl("http://abc.html");
  }

  /**
   * Executes runnable on UIThread
   * @param runnable
   * @param async
   */
  public static void runOnUIThread(final Runnable runnable, boolean async){
    if(Thread.currentThread() == Display.getDefault().getThread()){
      runnable.run();
    }else{
      if(async){
        Display.getDefault().asyncExec(new Runnable() {
          @Override
          public void run() {
            runnable.run();
          }
        });
      }else{
        Display.getDefault().syncExec(new Runnable() {
          @Override
          public void run() {
            runnable.run();
          }
        });
      }
    }
  }


  public static Browser createBrowserObject() {
    String browserDir = "D:/temporary/"+ System.currentTimeMillis();

    EngineOptions options = EngineOptions.newBuilder(HARDWARE_ACCELERATED)
        .userDataDir(Paths.get(browserDir))
        .remoteDebuggingPort(9223)//?
        .licenseKey(getLicenceKey())
        .build();

    engine = Engine.newInstance(options);
    return engine.newBrowser();
  }

  private static String getLicenceKey(){
    return "ABCD..";
  }
}

添加的maven依赖关系是

<dependency>
    <groupId>org.eclipse.swt</groupId>
    <artifactId>org.eclipse.swt.win32.win32.x86_64</artifactId>
    <version>4.3</version>
</dependency>
<dependency>
    <groupId>com.teamdev.jxbrowser</groupId>
    <artifactId>jxbrowser</artifactId>
    <version>7.1</version>
</dependency>
<dependency>
    <groupId>com.teamdev.jxbrowser</groupId>
    <artifactId>jxbrowser-javafx</artifactId>
    <version>7.1</version>
    <type>jar</type>
</dependency>
<dependency>
    <groupId>com.teamdev.jxbrowser</groupId>
    <artifactId>jxbrowser-win64</artifactId>
    <version>7.1</version>
</dependency>
<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>javafx</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${project.basedir}/lib/jfxswt.jar</systemPath>
</dependency>

异常是在调用loadUrl方法时发现的,以下是完整的内容: 踪迹:

线程 "JavaFX应用线程 "java.lang.IllegalStateException at com.teamdev.jxbrowser.internal.util.Preconditions.checkState(Preconditions.java:60) at com.teamdev.jxbrowser.internal.rpc.NewServiceConnection. invokeAsync(NewServiceConnection.java:261) at com.teamdev.jxbrowser.browser.internal.WindowedWidget.detach(WindowedWidget.java:60) at com.teamdev.jxbrowser.view.javafx.internal.HeavyweightWidget.detach(HeavyweightWidget. java:205) at com.teamdev.jxbrowser.view.javafx.internal.HeavyweightWidget.hide(HeavyweightWidget.java:193) at com.teamdev.jxbrowser.view.javafx.internal.HeavyweightWidget.close(HeavyweightWidget.java:112) at com.teamdev. jxbrowser.view.javafx.internal.Platform.lambda$execute$0(Platform.java:119) at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) at java.security.AccessController.doPrivileged(Native Method)。

jxbrowser
1个回答
0
投票

不幸的是,JxBrowser 7不支持JavaFXSWT和SwingSWT组合。这可能是你得到这些错误的一个原因。

原生SWT支持正在开发中,但尚未发布。

© www.soinside.com 2019 - 2024. All rights reserved.