Reputation: 334
I'm trying a simple project in JAVAFX using Intelij Idea, so i have 2 cases of problems:
case 1 :
When i put sample.fxml
and controller.java
and main.java
in the same folder src/main/java/FX/
:
1- SceneBuilder find controller.java
2- Main.java not find the FXML file
Parent root = FXMLLoader.load(getClass().getResource("/FX/sample.fxml"));
/opt/java/jdk1.8.0_20/bin/java -Didea.launcher.port=7537 -Didea.launcher.bin.path=/opt/idea-IU-143.381.42/bin -Dfile.encoding=UTF-8 -classpath /opt/java/jdk1.8.0_20/jre/lib/charsets.jar:/opt/java/jdk1.8.0_20/jre/lib/deploy.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/cldrdata.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/dnsns.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/jfxrt.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/localedata.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/nashorn.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/sunec.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/sunjce_provider.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/sunpkcs11.jar:/opt/java/jdk1.8.0_20/jre/lib/ext/zipfs.jar:/opt/java/jdk1.8.0_20/jre/lib/javaws.jar:/opt/java/jdk1.8.0_20/jre/lib/jce.jar:/opt/java/jdk1.8.0_20/jre/lib/jfr.jar:/opt/java/jdk1.8.0_20/jre/lib/jfxswt.jar:/opt/java/jdk1.8.0_20/jre/lib/jsse.jar:/opt/java/jdk1.8.0_20/jre/lib/management-agent.jar:/opt/java/jdk1.8.0_20/jre/lib/plugin.jar:/opt/java/jdk1.8.0_20/jre/lib/resources.jar:/opt/java/jdk1.8.0_20/jre/lib/rt.jar:/home/azb/IdeaProjects/TestMavenFX/target/classes:/opt/idea-IU-143.381.42/lib/idea_rt.jar com.intellij.rt.execution.application.AppMain FX.Main
** (java:7837): WARNING **: Couldn't connect to accessibility bus: Failed to connect to socket /tmp/dbus-MBNrjIF7ES: Connection refused
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$1/424058530.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3201)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3169)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3142)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3118)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3098)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3091)
at FX.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$55/669200515.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$51/645218408.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$53/222734631.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$52/444279218.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication.lambda$null$45(GtkApplication.java:126)
at com.sun.glass.ui.gtk.GtkApplication$$Lambda$43/1060865646.run(Unknown Source)
... 1 more
Process finished with exit code 1
case 2 :
When i put sample.fxml
in the folder src/main/resources/FX/
and controller.java
and main.java
in the folder src/main/java/FX/
:
1- SceneBuilder not find controller.java
2- Main.java find the FXML file, and the program run
Question : where's the wrong with this two cases ? witch cas is suppose to be the good one ? please help
Thanks
Upvotes: 1
Views: 12622
Reputation: 61
I had the same issue. Firstly I wrote my controller class on the fxml file manually, after that annotated @FXML at the top of the each method on the controller class than figured it out.
Upvotes: 0
Reputation: 1515
According to this answer the only is to put in the same folder where your FXML file exist,the problem is from scenebuilder it has a limitation .
chekc out the link below for more details
Upvotes: 2
Reputation: 1048
Define your controller class in your fxml file :
<AnchorPane xmlns:fx="http://javafx.com/fxml/1" id="GridPane" prefHeight="400.0" prefWidth="600.0" styleClass="mainFxmlClass" fx:controller="yourpackage.yourControllerClass">
<children>
...
</children>
</AnchorPane>
Upvotes: 2