JavaFX和Swing可调整大小的JFXPanel

时间:2016-03-01 17:49:39

标签: java swing javafx jfxpanel

我想在InternalFrame中使用可调整大小的webview,但是我崩溃了。这是我的代码:

/**
 * Create the frame.
 */
public TestBtn() {
    setClosable(true);
    setResizable(true);
    setMaximizable(true);
    setIconifiable(true);
    getContentPane().setBackground(Color.GRAY);

    JFXPanel panel = new JFXPanel();
    getContentPane().add(panel, BorderLayout.CENTER); 
    getContentPane().add(panel, BorderLayout.CENTER);
    setBounds(100, 100, 764, 546);      
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            initFX(panel);
        }
    });
    this.addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            initFX(panel);
        }
    });
}
private static void initFX(JFXPanel fxPanel) {
    // This method is invoked on the JavaFX thread
    Scene scene = createScene();
    fxPanel.setScene(scene);

}

private static Scene createScene() {
    Group  root  =  new  Group();
    Scene  scene  =  new  Scene(root);
    WebView view = new WebView();
    WebEngine engine = view.getEngine();
    engine.onResizedProperty();
    engine.load("http://google.bg");
    root.getChildren().add(view);

    return (scene);
}

public static void main(String[] args) {
     SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            try {
                TestBtn frame = new TestBtn();
                frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

我尝试最大化InternalFrame窗口后出现此错误:

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.web.WebEngine.checkThread(Unknown Source)
at javafx.scene.web.WebEngine.<init>(Unknown Source)
at javafx.scene.web.WebEngine.<init>(Unknown Source)
at javafx.scene.web.WebView.<init>(Unknown Source)
at test.TestBtn.createScene(TestBtn.java:79)
at test.TestBtn.initFX(TestBtn.java:71)
at test.TestBtn.access$0(TestBtn.java:69)
at test.TestBtn$2$1.componentResized(TestBtn.java:63)
at java.awt.Component.processComponentEvent(Unknown Source)
at javafx.embed.swing.JFXPanel.processComponentEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

1 个答案:

答案 0 :(得分:0)

您正在尝试更改Swing线程上的JavaFX组件。我认为它在你的组件监听器中。试试这个。

panel.addComponentListener(new ComponentAdapter() {
    @Override
    public void componentResized(ComponentEvent e) {
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
              initFX(panel);
            }
        });
    }
});