0

I have a frame with a button. When I hit it then a new frame is created that embeds JFXPanel with a text. When I close the window and hit the button again then JFXPanel(text) doesn't show in frame. The problem seems to be only when I set default close operation - DISPOSE. Why is this happening? I really need your help please. Below is the code :

public class TestJavaFXInSwingAfterDisposing {

    public static void initAndShowGUI() {
        // This method is invoked on the EDT thread
        JFrame frame = new JFrame("Swing and JavaFX");
        final JFXPanel fxPanel = new JFXPanel();
        frame.add(fxPanel);
        frame.setSize(300, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.addWindowListener(new java.awt.event.WindowAdapter() {
            @Override
            public void windowClosed(java.awt.event.WindowEvent evt) {
                Platform.setImplicitExit(false);
            }    
        });

        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                initFX(fxPanel);
            }
       });
    }

    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, Color.ALICEBLUE);
        Text  text  =  new  Text();

        text.setX(40);
        text.setY(100);
        text.setFont(new Font(25));
        text.setText("Welcome JavaFX!");

        root.getChildren().add(text);

        return (scene);
    }    
}


public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
        TestJavaFXInSwingAfterDisposing test = new TestJavaFXInSwingAfterDisposing();
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                test.initAndShowGUI();
            }
        });       
    }                                        

    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new MainFrame().setVisible(true);
            }
        });
    }                     
    private javax.swing.JButton jButton1;                 
}
Lukas Rotter
  • 4,158
  • 1
  • 15
  • 35

1 Answers1

0

JavaFX IllegalStateException when disposing JFXPanel in Swing.

Finally i found the solution. This command works great only if it is invoked in the main method().

Community
  • 1
  • 1