2

I'm very new to JavaFX and I'm having problem using my custom class in FXML. The console keeps giving me this exception when trying to load main.fxml:

... 1 more
Caused by: java.lang.ClassNotFoundException: sample.View$BoardPane
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
...

I created BoardPane class as a subclass of FlowPane in package sample.View and referred to it in my FXML as the following:

<?import sample.View.BoardPane?>
...
<TitledPane expanded="true" collapsible="false" text="BoardPane" fx:id="centerTitledPane">
    <BoardPane fx:id="mechoBoardPane"/>
</TitledPane>
...

and the project structure looks like this:

  • resources
    • fxml
      • main.fxml
  • ...
  • src
    • ...
    • sample
      • ...
      • View
        • BoardPane

Can anyone please help me on this? I've been searching for some time and haven't found any explanation.

Tengyu Liu
  • 1,223
  • 2
  • 15
  • 36

1 Answers1

4

The package name "View" must be lower case. In your project, as well as in the fxml.

For more details see the method loadType of FXMLLoader.class:

private Class<?> loadType(String name, boolean cache) throws ClassNotFoundException {
    int i = name.indexOf('.');
    int n = name.length();
    while (i != -1
        && i < n
        && Character.isLowerCase(name.charAt(i + 1))) {  // <<<<<<<<<
        i = name.indexOf('.', i + 1);
    }

    if (i == -1 || i == 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;
}
Roland
  • 18,114
  • 12
  • 62
  • 93
  • 2
    Why was this restriction placed upon FXMLLoader? – Mitch Talmadge Dec 25 '15 at 12:02
  • 1
    This fixed the issue for me. Weird that this restriction is placed on the package names when creating custom components and embedding them in .fxml files, but does not matter if you use the `` style include. – gbmhunter Aug 25 '16 at 18:08