JavaFX launch failed : javafx.fxml.LoadException: - java

I have an issue with the FXMLLoader which is looking recurrent but I can't fix it despite all the things I read here.
Here is my error code :
javafx.fxml.LoadException:
/C:/Users/mouna/Desktop/DatabaseProject/databaseProject/bin/main/login.fxml:13
at javafx.fxml/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:932)
at javafx.fxml/javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:982)
at javafx.fxml/javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:229)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:754)
at javafx.fxml/javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2808)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2634)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3323)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3280)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3249)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3222)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3199)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3192)
at databaseProject/main.Main.start(Main.java:17)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:474)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:447)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:446)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
Caused by: java.lang.ClassNotFoundException: LoginController.java
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
at javafx.fxml/javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:930)
... 22 more
People are saying this issue is due to a wrong pathfile and I try to do it as simple as possible :
path structure
and my main class code
package main;
import javafx.fxml.FXML;
import javafx.stage.Stage;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
public class LoginController {
#FXML
private Button exitButton;
public void exitButtonOnAction(ActionEvent event) {
Stage stage = (Stage) exitButton.getScene().getWindow();
stage.close();
}
}
login.fxml code
<?import javafx.scene.image.Image?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.text.Font?>
<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" prefHeight="400.0" prefWidth="520.0" xmlns="http://javafx.com/javafx/16" xmlns:fx="http://javafx.com/fxml/1" fx:controller="LoginController.java">
Thank you if you have a specific solution because I am running out of idea.

This part of the stack trace you posted indicates the problem cause.
Caused by: java.lang.ClassNotFoundException: LoginController.java
FXMLLoader is looking for a class named java in package LoginController.
Change this part of your login.fxml file
fx:controller="LoginController.java"
to this
fx:controller="main.LoginController"
Now FXMLLoader will look for a class named LoginController in package main.

Related

SceneBuilder won't load my custom control which references another custom control via FXML

I have created an FXML-based custom control, which in turn references another FXML-based custom control. They all work just fine when I load them in eclipse, but when I try to import them into SceneBuilder, the outer control (the one containing the other one) does not get imported properly.
Here is a dramatically simplified example:
Widget.java:
package example;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;
public class Widget extends Pane{
public Widget() {
Logger logger = Logger.getLogger(Widget.class.getName());
try {
FXMLLoader loader= new FXMLLoader(Widget.class.getResource("Widget.fxml"));
Pane pane = loader.load();
this.getChildren().add(pane);
}
catch(Exception e) {
logger.log(Level.SEVERE, "Failed to load Widget", e);
}
}
}
Widget.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.control.Label?>
<?import example.SubWidget?>
<FlowPane xmlns:fx="http://javafx.com/fxml/1" >
<children>
<Label text="Widget"/>
<SubWidget></SubWidget>
</children>
</FlowPane>
SubWidget.java:
package example;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.Pane;
public class SubWidget extends Pane{
public SubWidget() {
Logger logger = Logger.getLogger(SubWidget.class.getName());
try {
FXMLLoader loader= new FXMLLoader(SubWidget.class.getResource("SubWidget.fxml"));
Pane pane = loader.load();
this.getChildren().add(pane);
}
catch(Exception e) {
logger.log(Level.SEVERE, "Failed to load SubWidget", e);
}
}
}
SubWidget.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.FlowPane?>
<?import javafx.scene.control.Label?>
<FlowPane xmlns:fx="http://javafx.com/fxml/1" >
<children>
<Label text="Subwidget"/>
</children>
</FlowPane>
I export these to a jar and attempt to import them via SceneBuilder's jar import functionality. When I do so, SubWidget gets imported and looks just fine. Widget will also show up as a control, but it will be completely empty.
When I check the log files I see that it is due to the FXML loader not being able to find the SubWidget class file:
Oct 29, 2020 4:28:30 PM example.Widget <init>
SEVERE: Failed to load Widget
javafx.fxml.LoadException:
file:/C:/Users/nate/AppData/Roaming/Scene%20Builder/Library/TestFxControl.jar!/example/Widget.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2848)
at javafx.fxml.FXMLLoader.processImport(FXMLLoader.java:2692)
at javafx.fxml.FXMLLoader.processProcessingInstruction(FXMLLoader.java:2661)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2517)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at example.Widget.<init>(Widget.java:27)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1009)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.instantiateWithFXMLLoader(JarExplorer.java:110)
at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.exploreEntry(JarExplorer.java:160)
at com.oracle.javafx.scenebuilder.kit.library.util.JarExplorer.explore(JarExplorer.java:70)
at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.exploreAndUpdateLibrary(LibraryFolderWatcher.java:325)
at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.runDiscovery(LibraryFolderWatcher.java:138)
at com.oracle.javafx.scenebuilder.kit.library.user.LibraryFolderWatcher.run(LibraryFolderWatcher.java:92)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: example.SubWidget
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javafx.fxml.FXMLLoader.loadTypeForPackage(FXMLLoader.java:2916)
at javafx.fxml.FXMLLoader.loadType(FXMLLoader.java:2905)
at javafx.fxml.FXMLLoader.importClass(FXMLLoader.java:2846)
... 24 more
It appears to me that the FXML loader is using a classloader which doesn't include other custom components, which is odd because if I bypassed the FXML in the Widget class and made it:
public class Widget extends FlowPane{
public Widget() {
this.getChildren().addAll(new Label("Widget"), new SubWidget());
}
}
Then it loads just fine in SceneBuilder. So it appears that the ClassLoader that actually loads Widget and the one that the FXMLLoader uses are not the same.
This could possibly be related to this unanswered question, though they aren't exactly the same thing.
I had the same issue and created a work around that is not pretty but works.
Create a jar that contains blank classes for the controls you can not load into Scene Builder, e.g:
package com.junglefinance.gui.controls;
import javafx.fxml.FXML;
import javafx.scene.control.TextField;
public class CategoryTextField extends TextField {
#FXML private TextField textField;
public CategoryTextField() {
}
}
Make sure the package matches the package of your target control.
Load this jar into Scene Builder. This will allow Scene Builder to generate your FXML ready to load into your program. When it is loaded it will pick up your target control rather than the dummy as the import is (in this case)
<?import com.junglefinance.gui.controls.CategoryTextField?>
Note the package.

JavaFX / Intellij errors

I'm trying to set up my programing environment in Java.
I Have Ubuntu 18.04LTS / Java SE Runtime Environment (build 13.0.1+9)/ Intellij/ JavaFX Scene Builder 11.0.0
I have followed this tutorial : https://www.youtube.com/watch?v=T3NlWMzPyXM
The following 3 files were created:
Main.java :
package sample;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Main extends Application {
#Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java :
package sample;
import javafx.event.ActionEvent;
public class Controller {
// I start writing the code here:
public void pressButton(ActionEvent event) {
System.out.println("You have pressed the button");
}
}
sample.fxml :
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<GridPane alignment="center" hgap="10" prefHeight="300.0" prefWidth="300.0" vgap="10" xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<Button fx:id="btn_msg" mnemonicParsing="false" onAction="#pressButton" prefHeight="109.0" prefWidth="121.0" text="Button" />
</children>
</GridPane>
When I run application I receive the following output errors:
/usr/lib/jvm/java-13-oracle/bin/java --add-modules javafx.base,javafx.graphics --add-reads javafx.base=ALL-UNNAMED --add-reads javafx.graphics=ALL-UNNAMED -Djava.library.path=/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib -javaagent:/snap/intellij-idea-community/185/lib/idea_rt.jar=34245:/snap/intellij-idea-community/185/bin -Dfile.encoding=UTF-8 -classpath "/home/leo/IdeaProjects/Java kurs kenis/FX_trial2/out/production/FX_trial2:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/src.zip:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx-swt.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.web.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.base.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.fxml.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.media.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.swing.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.controls.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.graphics.jar" -p /home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.base.jar:/home/leo/IdeaProjects/javafx-sdk-11.0.2/lib/javafx.graphics.jar sample.Main
Exception in Application start method
java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:464)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:363)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at java.base/sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:1051)
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:830)
Caused by: java.lang.IllegalAccessError: class com.sun.javafx.fxml.FXMLLoaderHelper (in unnamed module #0xc9d8652) cannot access class com.sun.javafx.util.Utils (in module javafx.graphics) because module javafx.graphics does not export com.sun.javafx.util to unnamed module #0xc9d8652
at com.sun.javafx.fxml.FXMLLoaderHelper.<clinit>(FXMLLoaderHelper.java:38)
at javafx.fxml.FXMLLoader.<clinit>(FXMLLoader.java:2056)
at sample.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.gtk.GtkApplication.lambda$runLoop$11(GtkApplication.java:277)
... 1 more
Exception running application sample.Main
Process finished with exit code 1
I can't find any solution for this. Can anybody help?
Finally I have worked the problem out.
I put the line:
--module-path /home/leo/MyApps/javafx-sdk-13.0.1/lib --add-modules=javafx.controls,javafx.fxml
here:
Run > Edit Configuration > VM options
and:
File > Project structure > Libraries > + choose the directory:
/home/leo/MyApps/javafx-sdk-13.0.1/lib
You used
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
Try:
Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));

Javafx Webview Problem Caused by: javafx.fxml.LoadException:

I am very new to java fx , I am just trying to add WebView component. So everytime adding WebView Intellij returns a problem.
Here is output:
Caused by: java.lang.RuntimeException: Exception in Application start method
at javafx.graphics/com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:900)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication$2(LauncherImpl.java:195)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: javafx.fxml.LoadException:
Here is my sample.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.web.WebView?>
<GridPane alignment="center" hgap="10" vgap="10" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/11.0.1" fx:controller="sample.Controller">
<children>
<WebView fx:id="myweb" prefHeight="200.0" prefWidth="200.0" />
</children>
</GridPane>
Here is my controller:
import javafx.fxml.FXML;
import javafx.scene.web.WebView;
public class Controller {
#FXML
private WebView myweb;
}
If I am adding another stuff, everything works well, but after adding WebView it returns error.
Also I heard that WebView is not being supported in jdk 11. Is that true?
Just add the word javafx.web to your VM Options after your modules.
You can use the following line of code in your VM Options and add any other modules you're using.
--add-modules javafx.controls,javafx.fxml,javafx.graphics,javafx.web

Why is my JavaFX controller not loading?

I'm hoping someone can either explain to me what I am doing wrong, or point me to the answer if this has been answered previously (I've been rooting around hereabouts for 2 days looking for an answer).
I am trying to create an demo application that launches a JavaFX GUI. When my app is ready to launch the GUI (the app has to take care of things in the background first), I call:
Application.launch(StarFXDemo.class, "");
Here is StarFXDemo.java:
package standalonedemo;
import java.io.IOException;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class StarFXDemo extends Application
{
#Override
public void start(Stage stage) throws Exception
{
Parent root = null;
try
{
root = FXMLLoader.load(getClass().getResource("StarFXDemoDoc.fxml"));
} catch (IOException ioe)
{
ioe.printStackTrace(System.out);
}
Scene scene = new Scene(root);
stage.setScene(scene);
stage.show();
}
}
StarFXDemoDoc.fxml is in the JAR file & is the same package as StarFXDemo (rather than in another package or in a sub package). So
getClass().getResource("/standalonedemo/resources/StarFXDemoDoc.fxml")
Comes back with a URL to the file.
StarFXDemoDoc.fxml is this:
<?xml version="1.0" encoding="UTF-8"?>
<?import standalonedemo.*?>
<?import javafx.geometry.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<TitledPane animated="false" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" text="FX Sim Demo"
xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="standalonedemo.StarFXDemoDocController">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<VBox prefHeight="85.0" prefWidth="322.0" spacing="5.0" style="-fx-background-color: coral;">
<children>
<Label text="Sim Title" />
<TextField fx:id="SimNameField" onMouseDragExited="#setSimulationName" />
</children>
<padding>
<Insets left="25.0" top="15.0" />
</padding>
</VBox>
<Button fx:id="BuildSphereButton" layoutX="14.0" layoutY="100.0" mnemonicParsing="false" onAction="#buildSphereAction" text="Build Sphere" />
</children></AnchorPane>
</content>
</TitledPane>
And StarFXDemoDocController.java is this:
package standalonedemo;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class StarFXDemoDocController implements Initializable
{
#FXML
private TextField SimNameField;
#FXML
private Button BuildSphereButton;
/**
* RequiredConstructor
*/
public StarFXDemoDocController() {}
#Override
public void initialize(URL url, ResourceBundle rb)
{
}
#FXML
private void buildSphereAction(ActionEvent event)
{
}
#FXML
private void setSimulationName()
{
}
}
There is nothing really in the methods yet because I'm just trying to get the GUI to appear at this time. I have the empty constructor, and the #FXML tags on the GUI elements with fx:id tags (as well as their corresponding methods).
When I run the program, at the point I call
FXMLLoader.load(getClass().getResource("/standalonedemo/resources/StarFXDemoDoc.fxml"));
it throws the following exception (this exception is no longer relevant, see the exception below):
I am assuming I have something formatted wrong, but I have no idea what it is.
Can anyone help me out here?
EDIT/UPDATE:
As per James_D's comments (thank you James, obviously I had been staring at that for way too long...), I fixed the obvious error in my controller and made SimNameField a TextField, rather than a Label (and updated the code block above to reflect this). I also changed the exception handling to use printStackTrace. The changes now get me the following stack trace:
javafx.fxml.LoadException: file:<properPathTo>/StandAloneDemo/dist/StandAloneDemo.jar!/standalonedemo/resources/StarFXDemoDoc.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.access$700(FXMLLoader.java:103)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:922)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at standalonedemo.StarFXDemo.start(StarFXDemo.java:35)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
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$49(GtkApplication.java:139)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: standalonedemo.StarFXDemoDocController starting from SystemClassLoader[55 modules] with possible defining loaders null and declared parents [org.netbeans.MainImpl$BootClassLoader#97e1986, ModuleCL#1fa50985[org.netbeans.api.annotations.common], ModuleCL#7251b0ac[org.openide.awt], ModuleCL#7d1344a8[org.netbeans.api.progress], ModuleCL#26b9eb92[org.netbeans.swing.plaf], ModuleCL#7feb85da[org.openide.dialogs], ModuleCL#15c98cea[org.openide.nodes], ModuleCL#ed11c24[org.openide.windows], ModuleCL#621f6603[org.netbeans.swing.tabcontrol], ModuleCL#2a3ed4eb[org.netbeans.swing.outline], ...40 more]
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:224)
at org.netbeans.ModuleManager$SystemClassLoader.loadClass(ModuleManager.java:722)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more
Caused by: java.lang.ClassNotFoundException: standalonedemo.StarFXDemoDocController
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.netbeans.ProxyClassLoader.loadClass(ProxyClassLoader.java:222)
... 25 more
So I see the ClassNotFoundException, which got me to start looking at my controller class some more, and I noticed that Scene Builder has all but decided that my TextField doesn't exist as a tagged item (when I assign the fx:id of simNameField to the TextField in Scene Builder, I get the "No injectable field..." warning, and right clicking on the fx:id field for the control only shows the buildSphereButton tag). So I removed the simNameField from the controller class, right clicked on the FXML file in NetBeans, and told it to Make Controller. It added the TextField back with the correct fx:id and #FXML tag. Scene Builder still thinks it's an fx:id without an injectable field.
So I obviously have some manner of disconnect between the FXML and the controller, but I have no clue what it could be?
UPDATE2:
Took out the first stack trace, as it was no longer relevant and updated a few lines of code (like adding the import for the package to the FXML).
Been playing with it. If I remove the controller reference, the GUI launches as expected (so we know the FXML file is loading). If I instantiate a Controller object (before I call FXMLLoader.load(<...>)), it instantiates as expected, and I can verify that it is not null through the NetBeans debugger, so I know that the controller class is compiled, and on the class path.
So why can't the FXMLLoader find it? Should I be using an alternative method to load it? I've thought about just creating the controller object in the code and setting it that way, but that seems to annoy NetBeans when it comes to the fx:controller statement (i.e. If you have elements in the FXML that require a controller, and that statement isn't in there, NetBeans flags it as incorrect).
In the FXML you have
<TextField fx:id="SimNameField" onMouseDragExited="#setSimulationName" />
but in the controller you have
#FXML
private Label SimNameField ;
This tries to assign a text field to a field of type Label, which will cause an error at run time.
FYI for anyone who runs across this, I did solve the problem after a fashion. I still have no idea why the posted setup did not work, I'm thinking it has something to do with paths and package references.
Anyway, I was able to make it work like this:
URL fxmlFile = getClass().getResource("/standalonedemo/resources/StarFXDemoDoc.fxml");
StarFXDemoDocController sdCon = new StarFXDemoDocController();
FXMLLoader loader = new FXMLLoader(fxmlFile);
loader.setController(sdCon);
root = (TitledPane) loader.load();
Now it works as expected.

How do I create a custom JavaFX component library?

I've been toying with JavaFX for a while now and now I wanted to share some of the components I have built. For ease of use, I would like to be able to distribute the components as libraries, so another user can just import my libraries into JavFX project. However, building a a library file to import into SceneBuilder (SB) proved to be much harder than I had expected. I tried following this blog post but I must be doing something wrong. I want to create my own custom component, using a .fxml file and a .jar file.
I tried building this component:
TestPane.java
package pack;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;
import java.io.IOException;
public class TestPane extends AnchorPane {
public TestPane(){
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("TestPane.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
#FXML protected void onClick(ActionEvent ae){
System.out.println("Click");
}
}
TestPane.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.40" xmlns:fx="http://javafx.com/fxml/1">
<Button fx:id="button" layoutX="6.0" layoutY="10.0" mnemonicParsing="false" text="Button" onAction="#onClick" />
</fx:root>
I use Eclipse Mars, created a new JavaFX Library project, created a new package called pack. In that package, I created the two components from above. I then right-clicked the project and selected "Export... -> Jar-file", and made no changes in the export dialogue.
I then created a new FXML project. I opened the .fxml file in SB and imported my exported .jar file, using the Import JAR/FXML File..." dialogue, and then dragged it into my view.
Here is the code for the test project I've created.
LibTest.java
package test;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class LibTest extends Application {
#Override
public void start(Stage primaryStage) throws IOException {
Parent root = FXMLLoader.load( getClass().getResource("LibTest.fxml") );
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
LibTest.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import pack.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="199.0" prefWidth="126.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.40">
<children>
<TestPane layoutX="34.0" layoutY="82.0" />
</children>
</AnchorPane>
I thought I had followed all the online guides and tutorials by now but there is something I must be missing. When I then try to launch my test project, I get the error bellow. I understand that Caused by: javafx.fxml.LoadException: TestPane is not a valid type. is the "key message", but what does it mean, and how do I fix this problem?
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: 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$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/1645995473.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: javafx.fxml.LoadException: TestPane is not a valid type.
/D:/WorkspaceDir/ProjectDir/bin/test/LibTest.fxml:11
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2778)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2708)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2531)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2445)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3218)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3179)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3152)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3128)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3108)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3101)
at test.LibTest.start(LibTest.java:15)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$53/198061478.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1079803749.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/237061348.run(Unknown Source)
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$145(WinApplication.java:101)
at com.sun.glass.ui.win.WinApplication$$Lambda$36/2117255219.run(Unknown Source)
... 1 more
Exception running application test.LibTest
What is it that I am missing? What causes this problem and how do I fix it?
The problem is caused by the compiler (or the VM, I don't know the proper timing) does not know how to create an object of type TestPane.
Note from the error log that it says that the error occurred at D:/WorkspaceDir/ProjectDir/bin/test/LibTest.fxml:11. If we look at the LibTest.fxml, row 11, we see that this is the row that tries to create the TestPane.
The solution to the problem is to add the library file into the build path of the Java project. This will give your project the proper knowledge of how to create an object of type TestPane. Right-click the project -> Build Path -> Configure Build Path... Then, under the Libraries-tab, click Add JARs... (or Add External JARs..., if the .jar file is not located within your workspace) and find your exported .jar file.

Categories

Resources