Rerun JavaFX application

95 views Asked by At

I'm developing a plugin for IntelliJ. It is a menu that has only one option, and clicking it starts a JavaFX application. The application starts normally, but when I close the application and start again I get the following error:

java.lang.IllegalStateException: Application launch must not be called more than once

It's a simple application, the code is as follows:

This is the code in the menu.

try {
    Application.launch(MyApplication.class);
} catch (Exception e) {
    e.printStackTrace();
}

This is the JavaFX Application.

public class MyApplication extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {

        try {
            FXMLLoader loader = new FXMLLoader(getClass().getResource("/fxml/main-window.fxml"));
            loader.setController(new MainController());

            AnchorPane root = loader.load();
            Scene scene = new Scene(root, 1000, 600);

            primaryStage.setScene(scene);
            primaryStage.setMaximized(false);
            primaryStage.setResizable(false);

            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

How can I rerun the application? I tried to follow some answers here on the site, but I did not understand them very well, nor could I execute them correctly.

0

There are 0 answers