使用Swing时的webEngine JavaFX问题

huangapple go评论102阅读模式
英文:

Problem with webEngine JavaFX using with swing

问题

我正在尝试使用JavaFX WebEngine将浏览器加载到Swing中,代码如下:

  1. SwingUtilities.invokeLater(() -> InitGUI());
  1. final JFXPanel jfxPanel = new JFXPanel();
  2. panel.add(jfxPanel);
  3. Platform.runLater(() -> {
  4. WebView webView = new WebView();
  5. jfxPanel.setScene(new Scene(webView));
  6. webView.getEngine().load("https://pl.wikipedia.org/wiki/London");
  7. });

在我的Swing应用程序中,网站显示正常,但是当我在加载的网站上移动鼠标时,IntelliJ抛出以下异常:

  1. Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)'
  2. at javafx.embed.swing.JFXPanel.sendMouseEventToFX(JFXPanel.java:426)
  3. at javafx.embed.swing.JFXPanel.processMouseEvent(JFXPanel.java:465)
  4. at java.desktop/java.awt.Component.processEvent(Component.java:6401)
  5. at java.desktop/java.awt.Container.processEvent(Container.java:2263)
  6. at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
  7. ...

我应该在哪里添加mouseEventListeners?整个GUI都在我的Main类中,没有任何按钮,只有JLabels和来自JavaFX的webView。错误仅在这个webView上出现,每次我移动鼠标/点击时都会出现。其他Swing组件没有抛出任何异常。

英文:

I'm trying to load browser using JavaFX WebEnginge to swing as below

  1. SwingUtilities.invokeLater(() -> InitGUI());
  1. final JFXPanel jfxPanel = new JFXPanel();
  2. panel.add(jfxPanel);
  3. Platform.runLater(() -> {
  4. WebView webView = new WebView();
  5. jfxPanel.setScene(new Scene(webView));
  6. webView.getEngine().load("https://pl.wikipedia.org/wiki/London");
  7. });

Websites shows in my swing application but when I'm moving a mouse on the loaded website IntelliJ throws

  1. Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)'
  2. at javafx.embed.swing.JFXPanel.sendMouseEventToFX(JFXPanel.java:426)
  3. at javafx.embed.swing.JFXPanel.processMouseEvent(JFXPanel.java:465)
  4. at java.desktop/java.awt.Component.processEvent(Component.java:6401)
  5. at java.desktop/java.awt.Container.processEvent(Container.java:2263)
  6. at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5012)
  7. at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
  8. at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
  9. at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4919)
  10. at java.desktop/java.awt.LightweightDispatcher.retargetMouseEnterExit(Container.java:4691)
  11. at java.desktop/java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4677)
  12. at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4528)
  13. at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4489)
  14. at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
  15. at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2764)
  16. at java.desktop/java.awt.Component.dispatchEvent(Component.java:4844)
  17. at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
  18. at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
  19. at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
  20. at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
  21. at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
  22. at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
  23. at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
  24. at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
  25. at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
  26. at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
  27. at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
  28. at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
  29. at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
  30. at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
  31. at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
  32. at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  33. at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I should add mouseEventListeners somewhere? Whole GUI is in my Main class, without any buttons, just JLabels and this webView from javaFX. Error occurs only on this webView every time I move mouse/click. Other swing components don't throw anything.

@edit
I haven't posted first line of exception

  1. Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)'

答案1

得分: 1

问题已解决

我已添加

  1. --module-path /Users/<user>/javafx-sdk-15/lib --add-modules=javafx.controls,javafx.fxml,javafx.web,javafx.swing

在运行 > 编辑配置 > VM 选项中

英文:

Problem solved

I've added

  1. --module-path /Users/&lt;user&gt;/javafx-sdk-15/lib --add-modules=javafx.controls,javafx.fxml,javafx.web,javafx.swing

in Run>Edit Configurations>VM options

答案2

得分: 0

  1. 添加这些模块并没有解决我的问题,因为我已经添加了它们。
  2. ## 解决方案
  3. 在[JavaFX Gradle插件][1]中不指定JavaFX版本对我有用。
  4. ```groovy
  5. javafx {
  6. version = "11.0.1" // ← 我移除了这一行
  7. modules = ['javafx.controls', ...]
  8. }

详情

我尝试使用CalendarFX;结果发现我尝试使用的版本11.8.3使用了JavaFX 13.0.1。

一旦我升级或移除了我包含的JavaFX版本,异常就消失了。

完整异常信息

  1. Exception in thread "AWT-EventQueue-0" java.lang.NoSuchMethodError: 'void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)'
  2. at javafx.swing/javafx.embed.swing.JFXPanel.sendMouseEventToFX(JFXPanel.java:426)
  3. at javafx.swing/javafx.embed.swing.JFXPanel.processMouseEvent(JFXPanel.java:465)
  4. at java.desktop/java.awt.Component.processEvent(Component.java:6400)
  5. at java.desktop/java.awt.Container.processEvent(Container.java:2263)
  6. at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5011)
  7. ...
  1. <details>
  2. <summary>英文:</summary>
  3. Adding the modules didn&#39;t solve it for me—I had already added them.
  4. ## A Solution
  5. Not specifying a JavaFX version in the [JavaFX Gradle Plugin][1] solved it for me.
  6. ``` groovy
  7. javafx {
  8. version = &quot;11.0.1&quot; // ← I removed this line
  9. modules = [ &#39;javafx.controls&#39;, ...
  10. }

Details

I was trying to use CalendarFX; turns out the version I was trying to use, 11.8.3, uses JavaFX 13.0.1.

Once I upped or removed the JavaFX version I was including, the exception went away.

Full Exception

  1. Exception in thread &quot;AWT-EventQueue-0&quot; java.lang.NoSuchMethodError: &#39;void com.sun.javafx.embed.EmbeddedSceneInterface.mouseEvent(int, int, boolean, boolean, boolean, int, int, int, int, boolean, boolean, boolean, boolean, boolean)&#39;
  2. at javafx.swing/javafx.embed.swing.JFXPanel.sendMouseEventToFX(JFXPanel.java:426)
  3. at javafx.swing/javafx.embed.swing.JFXPanel.processMouseEvent(JFXPanel.java:465)
  4. at java.desktop/java.awt.Component.processEvent(Component.java:6400)
  5. at java.desktop/java.awt.Container.processEvent(Container.java:2263)
  6. at java.desktop/java.awt.Component.dispatchEventImpl(Component.java:5011)
  7. at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2321)
  8. at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
  9. at java.desktop/java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4918)
  10. at java.desktop/java.awt.LightweightDispatcher.retargetMouseEnterExit(Container.java:4698)
  11. at java.desktop/java.awt.LightweightDispatcher.trackMouseEnterExit(Container.java:4676)
  12. at java.desktop/java.awt.LightweightDispatcher.processMouseEvent(Container.java:4527)
  13. at java.desktop/java.awt.LightweightDispatcher.dispatchEvent(Container.java:4488)
  14. at java.desktop/java.awt.Container.dispatchEventImpl(Container.java:2307)
  15. at java.desktop/java.awt.Window.dispatchEventImpl(Window.java:2772)
  16. at java.desktop/java.awt.Component.dispatchEvent(Component.java:4843)
  17. at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:772)
  18. at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
  19. at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
  20. at java.base/java.security.AccessController.doPrivileged(Native Method)
  21. at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
  22. at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:95)
  23. at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:745)
  24. at java.desktop/java.awt.EventQueue$5.run(EventQueue.java:743)
  25. at java.base/java.security.AccessController.doPrivileged(Native Method)
  26. at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
  27. at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:742)
  28. at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
  29. at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
  30. at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
  31. at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
  32. at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
  33. at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

答案3

得分: 0

以下是已翻译的内容:

  1. 我正在使用Maven构建项目,我的解决方案还涉及到依赖版本的问题,但与其删除版本号,我将每个JavaFX库都升级到了最新可用版本。在此之前,使用了较旧的版本,这些版本是问题的原因,所以我在`pom.xml`中编辑了`<version>`标签。
  2. 之前:
  3. <dependency>
  4. <groupId>org.openjfx</groupId>
  5. <artifactId>javafx-controls</artifactId>
  6. <version>13</version>
  7. </dependency>
  8. <dependency>
  9. <groupId>org.openjfx</groupId>
  10. <artifactId>javafx-fxml</artifactId>
  11. <version>13</version>
  12. </dependency>
  13. <dependency>
  14. <groupId>org.openjfx</groupId>
  15. <artifactId>javafx-swing</artifactId>
  16. <version>11</version>
  17. </dependency>
  18. <dependency>
  19. <groupId>org.openjfx</groupId>
  20. <artifactId>javafx-base</artifactId>
  21. <version>13</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>org.openjfx</groupId>
  25. <artifactId>javafx-graphics</artifactId>
  26. <version>13</version>
  27. </dependency>
  28. 之后:
  29. <dependency>
  30. <groupId>org.openjfx</groupId>
  31. <artifactId>javafx-controls</artifactId>
  32. <version>17.0.1</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.openjfx</groupId>
  36. <artifactId>javafx-fxml</artifactId>
  37. <version>17.0.1</version>
  38. </dependency>
  39. <dependency>
  40. <groupId>org.openjfx</groupId>
  41. <artifactId>javafx-swing</artifactId>
  42. <version>17.0.1</version>
  43. </dependency>
  44. <dependency>
  45. <groupId>org.openjfx</groupId>
  46. <artifactId>javafx-base</artifactId>
  47. <version>17.0.1</version>
  48. </dependency>
  49. <dependency>
  50. <groupId>org.openjfx</groupId>
  51. <artifactId>javafx-graphics</artifactId>
  52. <version>17.0.1</version>
  53. </dependency>
英文:

I am building with Maven, and my solution also had something to do with versions of dependencies, but instead of deleting the version, I upgraded every javafx library to the newest available version. Before that older versions were used which were the cause of the problem so in pom.xml I edited the &lt;version&gt; tags.

Before:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  3. &lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
  4. &lt;version&gt;13&lt;/version&gt;
  5. &lt;/dependency&gt;
  6. &lt;dependency&gt;
  7. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  8. &lt;artifactId&gt;javafx-fxml&lt;/artifactId&gt;
  9. &lt;version&gt;13&lt;/version&gt;
  10. &lt;/dependency&gt;
  11. &lt;dependency&gt;
  12. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  13. &lt;artifactId&gt;javafx-swing&lt;/artifactId&gt;
  14. &lt;version&gt;11&lt;/version&gt;
  15. &lt;/dependency&gt;
  16. &lt;dependency&gt;
  17. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  18. &lt;artifactId&gt;javafx-base&lt;/artifactId&gt;
  19. &lt;version&gt;13&lt;/version&gt;
  20. &lt;/dependency&gt;
  21. &lt;dependency&gt;
  22. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  23. &lt;artifactId&gt;javafx-graphics&lt;/artifactId&gt;
  24. &lt;version&gt;13&lt;/version&gt;

After:

  1. &lt;dependency&gt;
  2. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  3. &lt;artifactId&gt;javafx-controls&lt;/artifactId&gt;
  4. &lt;version&gt;17.0.1&lt;/version&gt;
  5. &lt;/dependency&gt;
  6. &lt;dependency&gt;
  7. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  8. &lt;artifactId&gt;javafx-fxml&lt;/artifactId&gt;
  9. &lt;version&gt;17.0.1&lt;/version&gt;
  10. &lt;/dependency&gt;
  11. &lt;dependency&gt;
  12. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  13. &lt;artifactId&gt;javafx-swing&lt;/artifactId&gt;
  14. &lt;version&gt;17.0.1&lt;/version&gt;
  15. &lt;/dependency&gt;
  16. &lt;dependency&gt;
  17. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  18. &lt;artifactId&gt;javafx-base&lt;/artifactId&gt;
  19. &lt;version&gt;17.0.1&lt;/version&gt;
  20. &lt;/dependency&gt;
  21. &lt;dependency&gt;
  22. &lt;groupId&gt;org.openjfx&lt;/groupId&gt;
  23. &lt;artifactId&gt;javafx-graphics&lt;/artifactId&gt;
  24. &lt;version&gt;17.0.1&lt;/version&gt;
  25. &lt;/dependency&gt;

huangapple
  • 本文由 发表于 2020年9月23日 20:21:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64027861.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定