Unable to resolve dependency Java FX spring boot - spring-boot

I am new to JavaFX and Spring Boot. I am trying to use Dependency Injection but dependency is not getting Autowired. can anybody please help.
Thanks in Advance. Here is my code
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
#SpringBootApplication
#ComponentScan("com.tillster.kisok.installer")
public class Main extends AbstractJavaFxApplicationSupport {
public static void main(String[] args) {
System.setProperty("com.sun.javafx.touch", "true");
System.setProperty("com.sun.javafx.isEmbedded", "true");
System.setProperty("com.sun.javafx.virtualKeyboard", "none");
launch(Main.class, KisokInstallDetailView.class, args);
}
}
Here is my view File
import org.springframework.stereotype.Component;
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLView;
#Component
#FXMLView(value="/view/LanguageMainScreen.fxml", bundle="bundles.locale")
public class KisokInstallDetailView extends AbstractFxmlView {
}
Configuration File
#Configuration
#ComponentScan("com.kisok.installer")
public class KISOKConfiguration {
}
}
Controller
#FXMLController
#Component
public class KisokInstallProgressController implements Initializable {
#FXML
VBox hBox;
#FXML
WebView webView;
#Autowired
#Qualifier("installStepService")
InstallStepsService installService;
......................
#Override
public void initialize(URL location, ResourceBundle resources) {
Stage stage = Main.getStage();
WebEngine webEngine = webView.getEngine();
URL url = this.getClass().getClassLoader().getResource("test.html");
webEngine.load(url.toString());
Screen screen = Screen.getPrimary();
Rectangle2D bounds = screen.getVisualBounds();
double screenWidth = bounds.getWidth();
double screenHeight = bounds.getHeight();
webView.setPrefHeight(screenHeight);
webView.setPrefWidth(screenWidth);
hBox.setPrefWidth(screenWidth);
hBox.setPrefHeight(screenHeight);
stage.setFullScreen(true);
stage.setMaximized(true);
stage.setAlwaysOnTop(true);
InstallKIOSKDetails installKISOKDtls = installService.getKIOSKDetails();
System.out.println(installKISOKDtls.getInstallSteps().length);
}
Service Class:
import java.io.File;
import java.io.IOException;
import org.springframework.stereotype.Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tillster.kisok.installer.domain.InstallKIOSKDetails;
#Service("installStepService")
public class InstallStepsServiceImpl implements InstallStepsService {
private ObjectMapper objectMapper;
#Override
public InstallKIOSKDetails getKIOSKDetails() {
System.out.println("Inside getKIOSKDetails");
objectMapper = new ObjectMapper();
InstallKIOSKDetails installKISOKDtls = null;
File jsonInputFile = new File("E:\\KisokDeployment\\json.txt");
try {
installKISOKDtls = objectMapper.readValue(jsonInputFile, InstallKIOSKDetails.class);
} catch (IOException e) {
e.printStackTrace();
}
return installKISOKDtls;
}
}
Here's the Exception:
javafx.fxml.LoadException:
/E:/mna%20(3)/installer/target/classes/kioskInstallProgress.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
Caused by: java.lang.NullPointerException
at com.tillster.kisok.installer.controller.KisokInstallProgressController.initialize(KisokInstallProgressController.java:67)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
... 64 more
FXML
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.web.WebView?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8.0.141" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.tillster.kisok.installer.controller.KisokInstallProgressController">
<children>
<MenuBar VBox.vgrow="NEVER" />
<AnchorPane maxHeight="-1.0" maxWidth="-1.0" prefHeight="-1.0" prefWidth="-1.0" VBox.vgrow="ALWAYS">
<children>
<WebView fx:id="webView" layoutX="1.0" />
</children>
<children>
<VBox fx:id="hBox" style="-fx-alignment:center;">
<HBox style="-fx-alignment:center;">
<VBox fx:id="vBox" layoutX="65.0" layoutY="61.0" prefHeight="819.0" prefWidth="627.0" style="-fx-background-color: #EEE;">
<children>
<HBox prefHeight="70.0" prefWidth="627.0">
<children>
<ImageView fx:id="logo" fitHeight="92.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
<Label style="-fx-font-size: 20; -fx-padding: 30 5 5 5;" text="Starting Process of Installation" />
</children>
</HBox>
<HBox prefHeight="33.0" prefWidth="627.0" />
</children></VBox>
</HBox>
</VBox>
</children>
</AnchorPane>
</children>
</VBox>

Related

JavaFX Image and ImageViewer not supporting jpeg

I created an fxml file with an ImageView on an AnchorPane using SceneBuilder and through controller class I set the image with the code somewhat like...
#FXML ImageView;
...
void func(File file) {
Image image = new Image(file.toURI().toString());
ImageView.setImage(image);
}
But the thing is whenever the file is a jpeg it doesn't show. Though it works completely fine with png. I looked up the openjfx doc for Image. It says that jpeg's are supported. What's the catch here?
FXML code (test.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.Testctrl">
<children>
<ImageView fx:id="imageView" fitHeight="1080.0" fitWidth="1440.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
Controller Class code (Testctrl.java):
package client;
import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.File;
public class Testctrl {
#FXML ImageView imageView;
private Main main;
public void setMain(Main main) {
this.main = main;
}
public void load() {
File file = new File("image.jpg");
Image image = new Image(file.toURI().toString());
imageView.setImage(image);
imageView.setFitHeight(1080);
imageView.setPreserveRatio(true);
}
}
Main Class code (Main.java):
package client;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main extends Application {
private Testctrl testctrl;
private Stage stage;
#Override
public void start(Stage primaryStage) throws Exception{
stage = primaryStage;
stage.setFullScreen(true);
stage.setResizable(false);
showTest();
}
public void showTest(){
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("test.fxml"));
Parent root=null;
try {
root = loader.load();
}
catch (IOException e)
{
e.printStackTrace();
System.exit(-1);
}
testctrl = loader.getController();
testctrl.setMain(this);
testctrl.load();
stage.setScene(new Scene(root, 1920, 1080));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}

Dependency injection with JavaFX gives NullPointerExceptions

I want to mix Java FX and Spring Data. I found video about this problem https://www.youtube.com/watch?v=u0dEf-QN-90&t=3s and I implemented it in my code. In consequence my UI working but I couldn't inject dependency to TaskRepository. I had been trying in difrent ways to inject anything and always I get same error, that my dependency is null. What am I doing wrong?
addingWindow.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Slider?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.layout.VBox?>
<HBox maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="pl.projektyjava.listToDoJavaFX.fxmlcontrollers.AddingWindowController">
<children>
<VBox prefHeight="400.0" prefWidth="473.0" HBox.hgrow="ALWAYS">
<children>
<Label alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Tytuł" textAlignment="CENTER" />
<TextField fx:id="title" VBox.vgrow="ALWAYS" />
<Label alignment="TOP_CENTER" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" text="Opis" textAlignment="CENTER" />
<TextArea fx:id="description" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
<HBox maxHeight="-Infinity" VBox.vgrow="ALWAYS">
<children>
<Button fx:id="listOfQuest" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Lista Zadań" HBox.hgrow="ALWAYS" />
<Button fx:id="addQuest" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" mnemonicParsing="false" text="Dodaj zadanie" HBox.hgrow="ALWAYS" />
</children>
</HBox>
</children>
</VBox>
<Slider fx:id="priority" blockIncrement="1.0" majorTickUnit="1.0" max="5.0" maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minorTickCount="0" orientation="VERTICAL" showTickLabels="true" showTickMarks="true" snapToTicks="true" HBox.hgrow="ALWAYS" />
</children>
</HBox>
pl.projektyjava.listToDoJavaFX.appinitialization. Initialization
package pl.projektyjava.listToDoJavaFX.appinitialization;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
#Component
public class Initialization implements ApplicationListener<StarterClass.StageReadyEvent>{
#Override
public void onApplicationEvent(StarterClass.StageReadyEvent event) {
Stage stage = event.getStage();
HBox mainPane= null;
try {
mainPane = FXMLLoader.load(getClass().getResource("/addingWindow.fxml"));
} catch (IOException e) {
e.printStackTrace();
}
Scene scene=new Scene(mainPane);
stage.setScene(scene);
stage.show();
}
}
pl.projektyjava.listToDoJavaFX.appinitialization.ListToDoJavaFXApplication
package pl.projektyjava.listToDoJavaFX.appinitialization;
import javafx.application.Application;
import org.springframework.boot.autoconfigure.SpringBootApplication;
#SpringBootApplication
public class ListToDoJavaFxApplication {
public static void main(String[] args) {
Application.launch(StarterClass.class, args);
}
}
pl.projektyjava.listToDoJavaFX.appinitialization.StarterClass
package pl.projektyjava.listToDoJavaFX.appinitialization;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.stage.Stage;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
#Component
public class StarterClass extends Application {
private ConfigurableApplicationContext applicationContext;
#Override
public void init() {
applicationContext=new SpringApplicationBuilder(ListToDoJavaFxApplication.class).run();
}
#Override
public void start(Stage stage) {
applicationContext.publishEvent(new StageReadyEvent(stage));
}
#Override
public void stop() {
applicationContext.close();
Platform.exit();
}
static class StageReadyEvent extends ApplicationEvent {
public StageReadyEvent (Stage stage){
super(stage);
}
public Stage getStage(){
return ((Stage) getSource());
}
}
}
pl.projektyjava.listToDoJavaFX.body.Task
package pl.projektyjava.listToDoJavaFX.body;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
#Entity
public class Task {
#Id
#GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String description;
private double priority;
public Task(String title, String description, double priority) {
this.title = title;
this.description = description;
this.priority = priority;
}
public Task() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public double getPriority() {
return priority;
}
public void setPriority(double priority) {
this.priority = priority;
}
}
pl.projektyjava.listToDoJavaFX.body.TaskRepository
package pl.projektyjava.listToDoJavaFX.body;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
#Repository
public interface TaskRepository extends CrudRepository<Task, Long> {
}
pl.projektyjava.listToDoJavaFX.fxmlcontrollers.AddingWindowController
package pl.projektyjava.listToDoJavaFX.fxmlcontrollers;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Slider;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import pl.projektyjava.listToDoJavaFX.body.Task;
import pl.projektyjava.listToDoJavaFX.body.TaskRepository;
#Controller
public class AddingWindowController {
#Autowired
TaskRepository taskRepository;
#FXML
private TextField title;
#FXML
private TextArea description;
#FXML
private Button listOfQuest;
#FXML
private Button addQuest;
#FXML
private Slider priority;
public void initialize() {
addQuest.setOnAction(event -> {
String titleText = title.getText();
String descriptionText = description.getText();
double priorityVal = priority.getValue();
Task task = new Task(titleText, descriptionText, priorityVal);
taskRepository.save(task);
});
}
}
and ERROR:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Cannot invoke "pl.projektyjava.listToDoJavaFX.body.TaskRepository.save(Object)" because "this.taskRepository" is null
at pl.projektyjava.listToDoJavaFX.fxmlcontrollers.AddingWindowController.lambda$initialize$0(AddingWindowController.java:41)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8792)
at javafx.scene.control.Button.fire(Button.java:203)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3861)
at javafx.scene.Scene.processMouseEvent(Scene.java:1854)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2587)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:409)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:299)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:447)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:413)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:446)
at com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at com.sun.glass.ui.View.notifyMouse(View.java:942)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
After adding ControlerFactory to FXMLLoader I have this:
javafx.fxml.LoadException:
/B:/Projekty/1%20PROJEKT/listToDo-JavaFX/listToDo-JavaFX/target/classes/addingWindow.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2517)
at pl.projektyjava.listToDoJavaFX.appinitialization.Initialization.onApplicationEvent(Initialization.java:28)
at pl.projektyjava.listToDoJavaFX.appinitialization.Initialization.onApplicationEvent(Initialization.java:14)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378)
at pl.projektyjava.listToDoJavaFX.appinitialization.StarterClass.start(StarterClass.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:849)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'pl.projektyjava.listToDoJavaFX.fxmlcontrollers.AddingWindowController' 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:1172)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:940)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:982)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:229)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
... 19 more
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
2021-04-23 19:47:00.791 INFO 12928 --- [lication Thread] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2021-04-23 19:47:00.794 INFO 12928 --- [lication Thread] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2021-04-23 19:47:00.800 INFO 12928 --- [lication Thread] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:903)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:198)
at java.base/java.lang.Thread.run(Thread.java:831)
Caused by: java.lang.NullPointerException: Root cannot be null
at javafx.scene.Scene.<init>(Scene.java:345)
at javafx.scene.Scene.<init>(Scene.java:207)
at pl.projektyjava.listToDoJavaFX.appinitialization.Initialization.onApplicationEvent(Initialization.java:32)
at pl.projektyjava.listToDoJavaFX.appinitialization.Initialization.onApplicationEvent(Initialization.java:14)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:169)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:143)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:421)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:378)
at pl.projektyjava.listToDoJavaFX.appinitialization.StarterClass.start(StarterClass.java:26)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:849)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
... 1 more
Process finished with exit code 1
By default, the FXMLLoader will create controller instances by calling the no-argument constructor. Consequently, the AddingWindowController instance that's created is not managed by Spring, and Spring cannot inject #Autowired dependencies into it. (Basically, the Spring ApplicationContext has no knowledge that the AddingWindowController instance exists.)
You can supply a ControllerFactory to the FXMLLoader which asks that the controller is created by the spring application context:
#Component
public class Initialization implements ApplicationListener<StarterClass.StageReadyEvent>{
#Autowired
private ApplicationContext applicationContext ;
#Override
public void onApplicationEvent(StarterClass.StageReadyEvent event) {
Stage stage = event.getStage();
HBox mainPane= null;
try {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/addingWindow.fxml"));
fxmlLoader.setControllerFactory(applicationContext::getBean);
mainPane = fxmlLoader.load();
} catch (IOException e) {
e.printStackTrace();
}
Scene scene=new Scene(mainPane);
stage.setScene(scene);
stage.show();
}
}
Also note that you should create a new controller instance each time the FXML is loaded, so JavaFX controllers should have prototype scope. It probably doesn't matter too much, but the #Controller annotation in Spring is really intended for MVC controllers in a web application, so the #Component stereotype is really more appropriate here:
#Component
#Scope("prototype")
public class AddingWindowController {
// ...
}
By default #SpringBootApplication does componentscan in its own folder and underneath. So just place ur ListToDoJavaFxApplication one folder up into pl.projektyjava.listToDoJavaFX
Alternatively just add #ComponentScan(“pl.projektyjava.listToDoJavaFX”) right above #SpringBootApplication defining component scan explicitly.
In general whenever auto wiring fails silently without BeanCreationException at startup it is a sign that ur component is not being found by component scan

spring aop logging advice is not getting called

I am writing simple aspect with simple logging advice which will be called upon setter of class.
Below is code Snippet
AopMain.java
package com.example.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.example.test.model.Triangle;
import com.example.test.service.ShapeService;
public class AopMain {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//ShapeService shapeService = context.getBean("shapeService", ShapeService.class);
//shapeService.getTriangle().setName("triangle");
Triangle triangle = new Triangle();
triangle.setName("triangle class");
System.out.println(triangle.getName());
}
}
Triangle.java
package com.example.test.model;
public class Triangle {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println(name);
}
}
LoggingAspect.java
package com.example.test.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import com.example.test.model.Triangle;
#Component
#Aspect
public class LoggingAspect {
#Before("execution(public * *(..))")
public void LoggingAdvice(JoinPoint jPoint) {
System.out.println("Advice run. Get method is called " + jPoint.getTarget());
Triangle triangle = (Triangle) jPoint.getTarget();
}
#Before("args(name)")
public void test(String name) {
System.out.println("logging advice args " + name);
}
}
Output
triangle class
triangle class
spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
<!-- <bean id="triangle" class="com.example.test.model.Triangle">
<property name="name" value="Triangle Name"></property>
</bean> -->
<bean id="circle" class="com.example.test.model.Circle">
<property name="name" value="Circle Name"></property>
</bean>
<bean id="shapeService"
class="com.example.test.service.ShapeService" autowire="byType" />
<aop:aspectj-autoproxy />
<context:component-scan
base-package="com.example.test" />
</beans>
spring is working properly.
I do not understand why aspect is not gettting called.
please help me to understand this issue.

JavaFX MVC controller not work

i'm beginner with javaFX and i try to develop an application with MVC. In this application i use two controllers, one for menuBar and for panelCenter which contain button and label. When i click on Button (CenterControler) "It works" but when i click on File->Quit (MainControler) it don't works.
Here the file Main.java :
package application;
import java.io.IOException;
import controler.CenterControler;
import controler.MainControler;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
CenterControler ctrl_Center;
MainControler ctrl_Main;
Stage primaryStage;
#Override
public void start(Stage primaryStage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/themain.fxml"));
Parent root = (Parent) fxmlLoader.load();
ctrl_Main = (MainControler)fxmlLoader.getController();
Scene scene = new Scene(root, 800, 600);
primaryStage.setTitle("FXML Welcome");
primaryStage.setScene(scene);
primaryStage.show();}
public static void main(String[] args) {
launch(args);
}
}
MainControler.java
package controler;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
public class MainControler implements Initializable{
#FXML
private Menu File;
#FXML
private MenuBar barreMenu;
#FXML
private Menu sousMenu;
#FXML
private MenuItem menuQuit;
#FXML
private CenterControler centreControle;
#Override
public void initialize(URL location, ResourceBundle resources) {
assert File != null : "fx:id=\"File\" was not injected: check your FXML file 'themain.fxml'.";
assert menuQuit != null : "fx:id=\"menuQuit\" was not injected: check your FXML file 'themain.fxml'.";
}
public void setCenterControle(CenterControler cc){this.centreControle = cc; }
#FXML
protected void onClick2()
{
centreControle.show("IT WORKS !!! FROM MAIN CONTROLER");
}
}
CenterControler.java
package controler;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
public class CenterControler implements Initializable{
#FXML
private AnchorPane monFonds;
#FXML
private Button btn_1;
#FXML
private Label labelMSG;
#Override
public void initialize(URL arg0, ResourceBundle arg1) {
assert btn_1 != null : "fx:id=\"btn_1\" was not injected: check your FXML file 'part2.fxml'.";
assert labelMSG != null : "fx:id=\"labelMSG\" was not injected: check your FXML file 'part2.fxml'.";
}
public void setBTN(Button btn){ this.btn_1 = btn;}
public void setLabel(Label lb) {this.labelMSG = lb; }
public Button getBTN(){ return this.btn_1; }
public Label getLabel(){ return this.labelMSG;}
#FXML
protected void onClickShowMSG()
{
labelMSG.setText("PUSH FROM CENTER CONTROLER");
}
public void show(String msg)
{
labelMSG.setText(msg);
}
}
themain.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.paint.*?>
<?import javafx.scene.text.*?>
<VBox prefHeight="400.0" prefWidth="640.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler.MainControler">
<children>
<MenuBar fx:id="barreMenu" VBox.vgrow="NEVER">
<menus>
<Menu fx:id="sousMenu" mnemonicParsing="false" text="File">
<items>
<MenuItem fx:id="menuQuit" mnemonicParsing="false" onAction="#onClick2" text="Quit" />
</items>
</Menu>
</menus>
</MenuBar>
<fx:include fx:id="p2" source="part2.fxml" />
</children>
</VBox>
part2.fxml
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="controler.CenterControler">
<children>
<Button fx:id="btn_1" layoutX="274.0" layoutY="188.0" mnemonicParsing="false" onAction="#onClickShowMSG" text="Button" />
<Label fx:id="labelMSG" layoutX="286.0" layoutY="128.0" text="Label" />
</children>
</AnchorPane>
Thanks in advance.
The name of the field where the controller is injected is constructed by taking the value of the fx:id attribute of the fx:include element and concatenating "Controller". Therefore the name of the field where the nested controller is injected has to be p2Controller and not centreControle:
public class CenterControler implements Initializable {
//...
#FXML
private CenterControler p2Controller;
//...
}
Unfortunately this is not well documented... (I had to look at the source code to find out)

java fx can't image doesn't show

i have a problem with displaying images in java fx.I want to simply display image which i get from string path.My image in this case images.jpg is in src folder of the project.
so far i got code like this:
FXML FILE:
<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com /javafx/8">
<children>
<ImageView fx:id="imageView" fitHeight="150.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
CONTROLLER CLASS:
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
public class MainPaneController implements Initializable {
#FXML
private ImageView imageView;
private Image image;
String path;
#Override
public void initialize(URL location, ResourceBundle resources) {
String path = "indeks.jpg";
imageView = new ImageView();
image = new Image(path);
imageView.setImage(image);
}
public ImageView getImageView() {
return imageView;
}
public void setImageView(ImageView imageView) {
this.imageView = imageView;
}
public Image getImage() {
return image;
}
public void setImage(Image image) {
this.image = image;
}
}
MAIN CLASS:
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception {
Parent parent = (Parent) FXMLLoader.load(getClass().getResource(
"/pl/gallery/view/MainPane.fxml"));
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
I have no idea what i'm doing wrong here.

Resources