1

I have a swing application and have to integrate new javafx FXML files into it. I use a JFXPanel to open my FXML form. The exit button is created on the FXML form. My controller class implements the event of that button. But how do I close the Jframe from my swing application?

My JavaFX Controller class (method)

    @FXML
    private void doExit(ActionEvent event) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                Stage stage = (Stage) btnExit.getScene().getWindow();
                stage.close();
            }
        });
    }

Swing application start load my JFXPanel

JFrame frame = new JFrame("");
                frame.setLayout(new BorderLayout());
                final JFXPanel jfxPanel = new JFXPanel();
                frame.add(jfxPanel, BorderLayout.CENTER);
                frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
                frame.setVisible(true);
                SwingUtilities.invokeLater(() -> initPrijsMutatieFX(jfxPanel));
  • 2
    frame.dispose() ? – Gimby Jul 08 '19 at 11:53
  • The problem is that I should call something like frame.setvisible(false) from my javafx thread. but I cannot access my frame. From my swing application I can instantiate and start my JavaFx scene. If I want to close the scene (screen) the entire frame should be closed. – nico drapier Jul 08 '19 at 13:44
  • "but I cannot access my frame", I wouldn't know why you cannot access the frame. Just keep a reference to it somewhere. – Gimby Jul 08 '19 at 14:44
  • Thanks for the tip. I solved my problem. I added a referentie to the JFrame in my FXML Controller class. – nico drapier Jul 08 '19 at 19:01

0 Answers0