2

Does anyone know how to open a JavaFx application via rest requests?

Scenario: I have a Spring Boot Service that runs on every machine to make a bridge wiht my SPA(Single Page Application) and my comm ports. Thats because SPA cannot talk to the OS.

This communications are made via http requests.

Now I have a problem, I need to make a javafx application that is started via http request, when I call the firts time, it works ok, but if i close the javafx application clicking on 'x' and try to open again I'm getting the following error:

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

There is a way that when I close the javafx window, it kill the JavaFx Thread, so when I call it again, it start a new Thread? Or do I need to keep the Thread and just find a way to reopen my javafx application in the same Thread?

here is my @Controller

@RestController
@RequestMapping("v1/appfx")
public class AppfxController{
    @RequestMapping("")
    private void openJavaFxApp(){  
        try{       
            MyJavaFxApp.launch(MyJavaFxApp.class);
        }
        catch (Exception ex)
        {
            ex.printStackTrace();           
        }       
    }       
}

And Here is my JavaFx:

public class MyJavaFxApp extends Application {
    private final static Logger log =       LoggerFactory.getLogger(MyJavaFxApp.class);
    private BigDecimal left;
    private String selectedOperator;
    private boolean numberInputting;

    @FXML
    private TextField display;

    public MyJavaFxApp() {
        this.left = BigDecimal.ZERO;
        this.selectedOperator = "";
        this.numberInputting = false;
    }


    @Override
    public void start(Stage stage) throws Exception {
        stage.setTitle("MyJavaFxApp");
        stage.setOnCloseRequest(x -> {
            log.info("closed");
            Platform.exit();
        });
        stage.setResizable(false);
        stage.setScene(new Scene(FXMLLoader.load(getClass().getClassLoader().getResource("Test.fxml"))));
        stage.show();
        stage.toFront();
    }

}

Thanks for any help.

  • I don't know enough about the infrastructure you have to provide a complete answer, but you can only call [`launch()`](http://docs.oracle.com/javase/8/javafx/api/javafx/application/Application.html#launch-java.lang.String...-) once during the lifecycle of the Java Virtual Machine. So you probably need an `Application` subclass with an empty `start(..)` method, and call `launch(...)` specifying that class at the startup of the Spring Boot Service. Then your REST Controller should simply create and show a `Stage` (not call `launch()`). – James_D Dec 17 '16 at 04:26
  • Maybe have a look at my answer to http://stackoverflow.com/questions/32739199/javafx-software-design which does something vaguely similar in terms of lifecycle. – James_D Dec 17 '16 at 04:43
  • @James_D , starting an empty javafx application on my main(), and my controller creanting just creating a new Stage() (not calling launch() ) worked out for me! Thanks very much your the help! – Sydnei Peixoto Tanikawa Dec 17 '16 at 20:12

0 Answers0