open javafx application via rest call

357 views Asked by At

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.

0

There are 0 answers