Swing异常“未初始化工具包"中的javafx; [英] javafx in swing exception "Toolkit not initialized"

查看:65
本文介绍了Swing异常“未初始化工具包"中的javafx;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了该帖子: JavaFx 2.x-Swing:不在FX上应用程序线程

I've read the post: JavaFx 2.x - Swing : Not on FX application thread

参考 线程"AWT-EventQueue-0"中的异常java.lang.IllegalStateException:工具包未初始化"

with reference to " Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Toolkit not initialized "

我在这里找到了讨论 JavaFX 2.1:工具包未初始化

但我无法使用该解决方案

but I am not able to use the solution

在Swing事件分派器线程中实例化JFXPanel:"

"Istantiate JFXPanel in Swing Event Dispatcher Thread:"

因为它保持不确定的等待时间.

because it stay undefined time waiting.

在JDesktopPane中使用JInternalFrame时,我也遇到同样的问题.

I have your same problem using a JInternalFrame inside a JDesktopPane.

我尝试过:

final CountDownLatch latch = new CountDownLatch(1);
SwingUtilities.invokeLater(new Runnable() {
  @Override
  public void run() {
    final JFXPanel javafxPanel = new JFXPanel();
    latch.countDown();       
    BorderPane pane = new BorderPane();
    javafxPanel.setScene( new Scene(pane) {
      Text text = new Text("Hello World");            
    });
    frame.getContentPane().add(javafxPanel, BorderLayout.CENTER);
  }
});        
this.add(frame);
try {
    latch.await();
} catch (InterruptedException ex) {
    System.out.println("err");
    Logger.getLogger(WorkspacePanel.class.getName()).log(Level.SEVERE, null,     ex);
}

其中frame是JInternalFrame,这是JDesktopPane.

Where frame is a JInternalFrame and this is a JDesktopPane.

有什么帮助吗?预先感谢.

Any help ? Thanks in advance.

推荐答案

因为它保持不确定的等待时间.

because it stay undefined time waiting.

此等待可能是由latch引起的.如果您在事件调度线程上触发了您发布的代码,则latch.await()调用将阻止EDT,并确保您将不会执行您在EDT上发布的Runnable,因此永远不会到达latch.countDown()语句.您可以使用方法EventQueue.isDispatchThread快速检查这一点.

This waiting can be caused by the latch. If the code you posted is triggered on the Event Dispatch Thread, the latch.await() call will block the EDT and making sure the Runnable which you posted on the EDT will never executed, hence the latch.countDown() statement is never reached. You can quickly check this with the method EventQueue.isDispatchThread.

我强烈建议您简单地删除CountDownLatch,因为在另一个Thread上运行该代码是不可行的,因为您正在使用Swing组件,应在/swing组件上对其进行访问/修改/创建/....事件调度线程.

I would strongly suggest to simply remove the CountDownLatch, as running that code on another Thread is not an option since you are working with Swing components, which should be accessed/modified/created/... on the Event Dispatch Thread.

此外,一旦删除闩锁,您将遇到与链接的问题相同的异常.

Further, once you have removed the latch you will encounter the same exception as in the linked question. The

javafxPanel.setScene( new Scene(pane) {
  Text text = new Text("Hello World");            
});

必须在JavaFX线程上执行,而不是在事件调度线程上执行.有关该问题的解决方法,请参见我的答案.

must be executed on the JavaFX thread and not on the Event Dispatch Thread. See my answer on that question on how to solve that.

这篇关于Swing异常“未初始化工具包"中的javafx;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆