简体   繁体   English

JavaFx 2.x-Swing:不在FX应用程序线程上

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

I am trying to use JavaFx 2.x and Swing application by using a JInternalFrame in which attach a JFXPanel 我正在尝试通过使用JInternalFrame(在其中附加JFXPanel)来使用JavaFx 2.x和Swing应用程序

My code below 我的代码如下

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);
        }
    });
}                 
}

I have this 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)

For my purposes I must use a JInternalFrame: how can I solve this problem? 就我的目的而言,我必须使用JInternalFrame:如何解决此问题?

See the 'JavaFX in Swing' tutorial. 请参阅“ Swing中的JavaFX”教程。 You are performing JavaFX operations which should run on the JavaFX thread on the Swing thread (Event Dispatch Thread). 您正在执行应在Swing线程(事件调度线程)上的JavaFX线程上运行的JavaFX操作。

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. 那是您遇到的例外。

Use the Platform#runLater as shown in that tutorial 使用该教程中所示的Platform#runLater

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

The construction of the JFXPanel can remain on the EDT (which is also illustrated in that tutorial) JFXPanel的构造可以保留在EDT上(该教程中也对此进行了说明)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaFX 不在 FX 应用程序线程上 - JavaFX not on FX application thread JavaFX和套接字=不在FX应用程序线程上 - JavaFX and Sockets = Not on FX application thread Javafx使用计时器时不在fx应用程序线程上 - Javafx Not on fx application thread when using timer JavaFX:不在FX应用程序上发生线程错误 - JavaFX : Not on FX application thread error occurs Netty和JavaFX的“不在FX应用程序线程上”异常 - “Not on FX application thread” exception with Netty and JavaFX JavaFX在FX Application线程之外的另一个线程中运行WebEngine - JavaFX Running WebEngine in another thread than FX Application Thread Javafx 具有多个 windows 的应用程序,适用于 2 个不同的用户。 错误不在 FX 应用程序线程上; - Javafx Application with multiple windows for 2 different users . error Not on FX application thread; JavaFX中的Java AWT EventQueue“不在FX应用程序线程上”异常 - Java AWT EventQueue “Not on FX application thread” exception in JavaFX 在 init() 中使用 JavaFX Alert 时出现 IllegalStateException 因为不在 FX 应用程序线程上 - IllegalStateException when using JavaFX Alert in init() because not on FX application thread JavaFX在jdk7上工作正常,但在jdk8上工作不正常:在FX应用程序线程上不工作 - JavaFX works fine on jdk7 but not on jdk8: Not on FX application thread
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM