简体   繁体   中英

JavaFX and Swing resizable JFXPanel

I want a resizable webview in InternalFrame but what I have crashes. This is my code:

/**
 * 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();
            }
        }
    });
}

I get this error after I try to maximize the InternalFrame window:

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)

You're trying to alter JavaFX components on the Swing thread. I think it's in your component listener. Try this.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM