I am trying to create new application with JavaFX 2 and Spring Boot, but so far my simple app (like hello world) isn't running because of "root is null" in MainPaneController
.
MainPaneController class:
public class MainPaneController implements Initializable {
public static final String VIEW = "/fxml/Scene.fxml";
@FXML
private Node root;
@FXML
private Label label;
@PostConstruct
public void init() {
}
public Node getRoot() {
return root;
}
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
Main class FxBootApplication:
@SpringBootApplication
public class FxBootApplication extends Application {
private static String[] args;
@Override
public void start(final Stage stage) throws Exception {
//Parent root = FXMLLoader.load(getClass().getResource("/fxml/Scene.fxml"));
// Bootstrap Spring context here.
ApplicationContext context = SpringApplication.run(FxBootApplication.class, args);
MainPaneController mainPaneController = context.getBean(MainPaneController.class);
Scene scene = new Scene((Parent) mainPaneController.getRoot()); // error here
//Scene scene = new Scene(root);
//scene.getStylesheets().add("/styles/Styles.css");
stage.setTitle("JavaFX and Maven");
stage.setScene(scene);
stage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
public static void main(String[] args) {
FxBootApplication.args = args;
launch(args);
}
}
ApplicationConfiguration class:
@Configuration
public class ApplicationConfiguration {
@Bean
public MainPaneController mainPaneController() throws IOException {
MainPaneController mpc = (MainPaneController) loadController(MainPaneController.VIEW);
return mpc;
}
public <T> T loadController(String url) throws IOException {
try (InputStream fxmlStream = getClass().getResourceAsStream(url)) {
FXMLLoader loader = new FXMLLoader(getClass().getResource(url));
//FXMLLoader.load(url);
loader.load(fxmlStream);
return loader.getController();
}
}
}
Error is while I am trying to get root for Scene by controller.getRoot()
;
I followed the solution proposed here -> JavaFX fxml - How to use Spring DI with nested custom controls? but eventually is not working for me at all. Should I somehow initialize this root before?
Unfortunately I don't find the link to the solution, that works for me, anymore... But: I have the code, which I tested to some extend.
First you need your Application class:
I used
org.springframework.boot:spring-boot-starter-parent:1.3.3.RELEASE
as the base.Note the
FXMLLoaderService
interface I autowired here:The implementation looks like this:
The usage is already displayed in the Application class: Just @Autowire the service and create the sub-views from there. Since I rely almost exclusivly on FXML, this one is importand to me, since I want to use all that nice DI stuff in my Controllers.
Best example is the global Application, wich holds some JavaFX properties I attach to in the controller classes.
While the shown application is more a stub (the application and the FXML service), I had a fun project where I used this approach in concurrency to a parallel developed Web application, that was RESTing on a "Micro"service I developed at work.
Hope the code is enough an example to get it to work on your side. Please just ask, if you have more questions.
Cheers, Daniel
Edit: I think the mistake in your code is simply the part in the FXML service. I have this injected ConfigurableApplicationContext which I use to create the controller from. You would need a ControllerFactory for this.
There are some third party samples to assist with JavaFX and Spring Boot integration: