textfield - JavaFX: ClassNotFoundException for imported class in FXML -
i'm trying create textfield title embedded in field border like:
following solution posted here i've created .java file called titledborder.java
within src>main>java
directory. fxml in src>main>resources
directory , i've added:
<?import titledborder?>
@ top , shows no error like:
i added code fxml
<hbox prefheight="100.0" prefwidth="200.0"> <children> <titledborder title="email" > <textfield fx:id="emailfield" prefheight="44.0" prefwidth="143.0" /> </titledborder> </children> </hbox>
and shows no error either. launch main method in class in src>main>java gets error in .fxml saying javafx.fxml.loadexception: /c:/users/me/documents/automation/target/classes/demofxml.fxml
and
caused by: java.lang.classnotfoundexception @ javafx.fxml.fxmlloader.loadtype(fxmlloader.java:2899)
i'm not sure why references "/target/classes/..." opposed "/src/main/java/...".
this fxml example i've found i'm confused why i'm getting error upon compiling, yet no errors shown prior? removing reference titledborder
allows code function/compile properly. since in src package use code in fxml connect w/ controller fx:controller="loadcontroller">
. css added too.
thoughts?
the line
<?import titledborder?>
implies put titledborder.java
file default package (i.e. no package declaration in source code of file). fxmlloader
's source code checks imports in fxml file , splits package path name , class name in loadtype(...)
below, load imported class later loadtypeforpackage()
:
private class<?> loadtype(string name, boolean cache) throws classnotfoundexception { int = name.indexof('.'); int n = name.length(); while (i != -1 && < n && character.islowercase(name.charat(i + 1))) { = name.indexof('.', + 1); } if (i == -1 || == n) { throw new classnotfoundexception(); } string packagename = name.substring(0, i); string classname = name.substring(i + 1); class<?> type = loadtypeforpackage(packagename, classname); if (cache) { classes.put(classname, type); } return type; } // todo rename loadtype() when deprecated static version removed private class<?> loadtypeforpackage(string packagename, string classname) throws classnotfoundexception { return getclassloader().loadclass(packagename + "." + classname.replace('.', '$')); }
the imported class name "titledborder" variable i
@ 1st line in loadtype
method evaluated name.indexof('.') = -1
, , throw classnotfoundexception
in next lines of code.
generally bad practice use default packages. put titledborder.java package , import as
<?import my.some.package.titledborder?>
Comments
Post a Comment