How do I start again an external JavaFX program? Launch prevents this, even if the JavaFX program ended with Platform.Exit

坚强是说给别人听的谎言 提交于 2020-01-14 22:54:21

问题


From my MainProject (Java 8) i starts a JavaFX 8 Class.

public void startFX() {
    if (isRestartPrintModul() == true) {
        fxMain.init();
    } else {
        setRestartPrintModul(true);
        fxMain.main(new String[] {"ohne"});
    }
}

This is my FXMain:

        package quality;

    import javafx.application.Application;
    import javafx.application.Platform;
    import javafx.event.ActionEvent;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.layout.StackPane;
    import javafx.stage.Modality;
    import javafx.stage.Stage;
    import javafx.stage.WindowEvent;

    /**
     *
     * @author pu_laib
     */
    public class FXMain extends Application {

        private static Stage primaryStage;

        @Override
        public void init() {
            Platform.setImplicitExit(false);
            if (getPrimaryStage() != null) {
                getPrimaryStage().show();
            } else {
            }
        }

        @Override
        public void start(Stage primaryStage) {
            setPrimaryStage(primaryStage);
            // -> Applicationerror: getPrimaryStage().initModality(Modality.NONE);
            // -> Applicationerror: getPrimaryStage().initModality(Modality.APPLICATION_MODAL);
            Button btn = new Button();
            btn.setText("Say 'Hello World'");
            btn.setOnAction((ActionEvent event) -> {
                System.out.println("Hello World!");
            });

            StackPane root = new StackPane();
            root.getChildren().add(btn);

            Scene scene = new Scene(root, 300, 250);

            getPrimaryStage().setTitle("Hello World!");
            getPrimaryStage().setScene(scene);
            getPrimaryStage().show();
            this.primaryStage.setOnCloseRequest((WindowEvent event) -> {
                Platform.exit();
            });

        }

        public static void main(String[] args) {
            launch(args);
        }

        public Stage getPrimaryStage() {
            return primaryStage;
        }

        public void setPrimaryStage(Stage primaryStage) {
            this.primaryStage = primaryStage;
        }

    }

It is not possible to call the Print module from my MainProject again, although it is closed in my opinion.

Once the PrintModul module is completed, launch can not remember that it ran before, right?

What is wrong?

Thank you.


回答1:


The documentation for the Application::launch(args) method states:

It must not be called more than once or an exception will be thrown.

So:

  1. Just call launch once.
  2. Invoke Platform.setImplicitExit(false) so that the JavaFX runtime will continue running even if all stages are closed.
  3. Once the JavaFX application has finished doing its job, don't invoke Platform.exit(), but let the JavaFX platform continue running even when you aren't actively using it.
  4. Instead of trying to launch the application again, invoke the start() method of the application (or some other public method you provide on the application which accepts the parameters you want to pass) to "run" the application a second or more times (perhaps instantiating a new stage to pass to start method if needed).
  5. Once all of the work is complete, invoke Platform.exit() at that time to cleanly shutdown the JavaFX system.

Your other option would be to launch a new process instead of running the JavaFX applications inside the same process as your MainProject, but, in general, I'd recommend the approach outlined above rather than creating new processes.



来源:https://stackoverflow.com/questions/29766582/how-do-i-start-again-an-external-javafx-program-launch-prevents-this-even-if-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!