rvz
rvz

Reputation: 43

Spring Boot and JavaFX, using WeaverFX

I've created an JavaFX program with FXML. I was asked to make it into a Spring Boot application. I found this website to help me through it using WeaverFX. https://www.vojtechruzicka.com/javafx-spring-boot/ I followed the tutorial step by step and looking through my code, I have everything the same. Yet I get an error.

 @Override
    public void start(Stage stage) {
        FxWeaver fxWeaver = applicationContext.getBean(FxWeaver.class);
        Parent root = fxWeaver.loadView(MyController.class);
        Scene scene = new Scene(root);
        stage.setScene(scene);
        stage.show();
2020-04-08 11:41:16.728  INFO 14512 --- [JavaFX-Launcher] o.s.boot.SpringApplication               : Started application in 1.239 seconds (JVM running for 2.077)
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.myprogram.attempttwo.controller.MyController' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:351)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:342)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1126)
    at net.rgielen.fxweaver.core.FxWeaver.getBean(FxWeaver.java:295)
    at net.rgielen.fxweaver.core.FxWeaver.lambda$load$1(FxWeaver.java:381)
    at java.util.Optional.orElseGet(Optional.java:267)
    at net.rgielen.fxweaver.core.FxWeaver.load(FxWeaver.java:381)
    at net.rgielen.fxweaver.core.FxWeaver.loadView(FxWeaver.java:184)
    at net.rgielen.fxweaver.core.FxWeaver.loadView(FxWeaver.java:125)
    at net.rgielen.fxweaver.core.FxWeaver.loadView(FxWeaver.java:94)
    at com.myprogram.attempttwo.application.JavaFxApplication.start(JavaFxApplication.java:29)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$5(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$3(WinApplication.java:177)
    ... 1 more

It seems that its not using the 'loadView' but I don't know enough about Java to figure out why it is not. Or at least things to check.

***edit showing MyController.class per request

package com.myprogram.attempttwo.controller;

import com.myprogram.attempttwo.application.WeatherService;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
import net.rgielen.fxweaver.core.FxmlView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@FxmlView("main-stage.fxml")
public class MyController {

    @FXML
    private Label weatherLabel;
    private WeatherService weatherService;

    @Autowired
    public MyController(WeatherService weatherService) {
        this.weatherService = weatherService;
    }

    public void loadWeatherForecast(ActionEvent actionEvent) {
        this.weatherLabel.setText(weatherService.getWeatherForecast());
    }
}

Upvotes: 1

Views: 1312

Answers (1)

Shekhar Rai
Shekhar Rai

Reputation: 2058

I suspect that your project structure seems to cause the issue. I mean your main class is in the com.myprogram.attempttwo.application package and your controller classes are in the com.myprogram.attempttwo.controller package.

Please make sure that your main JavaFxApplication is in the root package to get initialized all its beans. For example, you can rename your controller package with com.myprogram.attempttwo.application.controller so that your controllers will be under the main class.

Upvotes: 1

Related Questions