How do I start an external JavaFX program again? Startup prevents this even if the JavaFX program exited with Platform.Exit
From my main project (Java 8) I am running the 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;
}
}
There is no way to call the Print module again from my MainProject even though it is closed in my opinion.
Once the PrintModul is finished, the startup won't be able to remember that it worked before, right?
What's wrong?
Thank.
+3
source to share
1 answer
The documentation for Application :: launch (args) states:
You cannot call it more than once or throw an exception.
So:
- Just call the launch once.
- Call
Platform.setImplicitExit(false)
for the JavaFX runtime to keep running even if all stages are closed. - Once the JavaFX application has finished doing its work, do not call
Platform.exit()
, but let the JavaFX platform continue to run even if you are not actively using it. - Instead of restarting the application, call an
start()
application method (or some other public method that you provide to the application that accepts parameters that you want to pass) to "run" the application for a second or more times (perhaps instantiating a new stage to jump to the startup method if needed). - Once all the work is complete, call
Platform.exit()
at this time to completely shut down the JavaFX system.
Another option is to start a new process instead of launching JavaFX applications in the same process as your main project, but in general I would recommend the approach described above rather than creating new processes.
+1
source to share