JavaFx 2.x - Swing : Not on FX application thread

标签 java swing exception-handling javafx-2

我正在尝试通过使用附加 JFXPanel 的 JInternalFrame 来使用 JavaFx 2.x 和 Swing 应用程序

下面是我的代码

public class InternalFrameWithJavafx extends javax.swing.JFrame {
/**
 * Creates new form InternalFrameWithJavafx
 */
public InternalFrameWithJavafx() {
    initComponents();

    final JInternalFrame frame = new JInternalFrame();
    frame.setTitle("test InternalFrame");
    frame.setVisible(true);        
    frame.setResizable(true);
    frame.setIconifiable(true);
    frame.setMaximizable(true);
    frame.setIconifiable(true);
    frame.setClosable(true);
    frame.setSize(800,600);
    frame.setLocation(0, 0);        
    frame.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    SwingUtilities.invokeLater(new Runnable() {

    @Override
    public void run() {
    final JFXPanel javafxPanel = new JFXPanel();

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

    });
    frame.getContentPane().add(javafxPanel, BorderLayout.CENTER);
    }
});        
    this.add(frame);    
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">                          
private void initComponents() {

    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 400, Short.MAX_VALUE)
    );
    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 300, Short.MAX_VALUE)
    );

    pack();
}// </editor-fold>                        

/**
 * @param args the command line arguments
 */
public static void main(String args[]) {
    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (UnsupportedLookAndFeelException e) {
        //handle exception
    } catch (ClassNotFoundException e) {
        //handle exception
    } catch (InstantiationException e) {
        //handle exception
    } catch (IllegalAccessException e) {
        //handle exception
    }
    java.awt.EventQueue.invokeLater(new Runnable() {

        public void run() {
            new InternalFrameWithJavafx().setVisible(true);
        }
    });
}                 
}

我有这个异常(exception)

Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Not on FX application thread; currentThread = AWT-EventQueue-0
at com.sun.javafx.tk.Toolkit.checkFxUserThread(Unknown Source)
at com.sun.javafx.tk.quantum.QuantumToolkit.checkFxUserThread(Unknown Source)
at javafx.scene.Scene.<init>(Unknown Source)
at javafx.scene.Scene.<init>(Unknown Source)
at javafxapplication2.InternalFrameWithJavafx$1$1.<init>(InternalFrameWithJavafx.java:47)
at javafxapplication2.InternalFrameWithJavafx$1.run(InternalFrameWithJavafx.java:47)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:705)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:675)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

出于我的目的,我必须使用 JInternalFrame:我该如何解决这个问题?

最佳答案

参见 'JavaFX in Swing'教程。您正在执行应该在 Swing 线程(事件调度线程)上的 JavaFX 线程上运行的 JavaFX 操作。

幸运的是,他们从之前的错误中吸取了教训,现在当您在错误的线程上执行操作时会抛出异常。那是你遇到的异常。

使用该教程中所示的 Platform#runLater

Platform.runLater(new Runnable() {
        @Override
        public void run() {
          //javaFX operations should go here
        }
   });

JFXPanel 的构造可以保留在 EDT 上(该教程中也有说明)

关于JavaFx 2.x - Swing : Not on FX application thread,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12182592/

相关文章:

java - 当您更改方法内的引用时会发生什么? (7)

java - GridLayout 仅在手动调整窗口大小时更新

java - 为什么 Windows XP 会在我的第二个屏幕上最小化我的 swing 全屏窗口?

c# - 访问参数的属性时使用 ArgumentNullException

java.lang.ClassCastException : cannot be cast to java. lang.Integer

java - Spring:使用 spring 的生命周期回调 'destroy-method' 比常规的 finalize() 方法有什么优势吗?

Java:方法的继承

java - 动态添加新的组合框和按钮到层次结构

c# - 为什么此方法返回 double.PositiveInfinity 而不是 DivideByZeroException?

c# - IncludeExceptionDetailInFaults 不像想象的那样表现