Im trying to host javafx application within a simple springboot application, using web and thymleaf.
It serves up static html to local host, and i can get this content both from the browser and trough the webview of a independant javafx, using webengine.load("localhost:port")
The problem happens when i try to instantiate a javafx application/bean in spring boot. I successfully instantiate the client, get the fxml and controller running fine, but the webengine.load method return an empty window. I tried loading content served up from separate running applications, and it works, so the problem is trying to load the content served up by the springbootapplication with a javafx client instantiated by the same springbootapplication.
The JAVAfx application is instantiated as a bean like so,
@Component
public class Cool extends Application {
public void start(Stage stage) throws IOException {
Parent parent = FXMLLoader.load(getClass().getResource("/rendum/webView.fxml"));
Scene scene = new Scene(parent, 300, 300);
stage.setScene(scene);
stage.show();
}
@Bean
public void go() {
launch();
}
}
the webcontent is attempted loaded like this
public class Controller {
@FXML
WebView webView;
public void initialize() {
System.out.println("Controller up");
WebEngine webEngine = webView.getEngine();
webEngine.load("http://localhost:8080/");
JSObject window = (JSObject) webEngine.executeScript("window");
window.setMember("java", new JSApi());
webEngine.setOnAlert((e)-> {
System.out.println(e.getData());
});
}
}
So just to confirm, i've tried changing the url to other running application and it works, and i can atm use this same exact controller setup in a different javafx application and get the content displayed.
Im using jdk 8. Does anyone know why the content can not get loaded? Entrypoint
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Controller instantiation. It serves html with som js at "/" (entrypoint)
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class ViewController {
@GetMapping("/")
public String index() {
return "index";
}
}
Project structure.
So it instantiete the controller that serves up html and js to localhost. This is unavailable for the webengine in the javafxbean, but it is available for applications outside this springboot application
go()(which you haven't shown). This doesn't make a whole lot of sense. Where are you starting Spring? TheApplicationclass (and thestart()method) is the entry point for the entire application. So if this is all you have, there is no Spring application context etc.launch()) is not really a supported use-case either.