不在FX应用程序线程上

不在FX应用程序线程上

本文介绍了JavaFx 2.x - Swing:不在FX应用程序线程上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaFx 2.x和Swing应用程序,使用JInternalFrame,其中附加JFXPanel

I am trying to use JavaFx 2.x and Swing application by using a JInternalFrame in which attach a JFXPanel

我的代码在

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 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:我如何解决这个问题?

For my purposes I must use a JInternalFrame: how can I solve this problem?

推荐答案

请参阅教程。您正在执行JavaFX操作,该操作应该在Swing线程(Event Dispatch Thread)上的JavaFX线程上运行。

See the 'JavaFX in Swing' tutorial. You are performing JavaFX operations which should run on the JavaFX thread on the Swing thread (Event Dispatch Thread).

幸运的是,他们从以前的错误中学到了,现在抛出异常对错误的线程执行操作。这是您遇到的例外。

Luckily they learnt from their previous mistakes and now throw exceptions when you perform operations on the wrong thread. That is the exception you encountered.

使用 Platform#runLater ,如该教程所示

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

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

The construction of the JFXPanel can remain on the EDT (which is also illustrated in that tutorial)

这篇关于JavaFx 2.x - Swing:不在FX应用程序线程上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 23:36