JavaFX SceneBuilder: ClassNotFoundException when loading custom control

这一生的挚爱 提交于 2021-01-28 11:30:36

问题


I see that there are many posts about this, some slightly recent, some much older. Nonetheless, I would like to share my own experience because I am facing this issue and I cannot seem to find a solution.

First of all, the context :

  • I use Intellij IDEA Community Edition 20018.2.2 from 21st of August 2018
  • I use Scene Builder from GLUON, version 8.5.0 (latest as of this post)
  • I use the JDK & JRE 64 bits version 8.181 (latest as of this post)

The situation: I am writing a chat app. I have a MainView which is divided into 3 components. In order to split the UI logic, these 3 components are as followed :

  1. The ConversationView which displays the list of conversation ;
  2. The ActiveConversationView which displays the currently selected conversation, whith all the messages ;
  3. The ToolBarView which sits on the top of the window and displays some icons with specific actions.

Working individually on these components is OK, both integrated Scene Builder (in Intellij) and GLUON Scene Builder open the FXMLs and display the content.

As you will understand, the MainView FXML is very straightforward :

<fx:root minHeight="-Infinity" minWidth="-Infinity" prefHeight="720.0" prefWidth="1280.0" type="AnchorPane" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
    <children>
        <ToolBarView fx:id="toolBarView" minHeight="-Infinity" minWidth="-Infinity" prefHeight="72.0" prefWidth="1280.0" AnchorPane.bottomAnchor="648.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0" />
        <ConversationsView fx:id="conversationsView" minHeight="-Infinity" minWidth="-Infinity" prefHeight="648.0" prefWidth="320.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="960.0" AnchorPane.topAnchor="72.0" />
        <ActiveConversationView fx:id="activeConversationView" prefHeight="648.0" prefWidth="960.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="320.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="72.0" />
    </children>
</fx:root>

As a precision, I work using the MVVM pattern, so all views have their corresponding view models, down to each cells in each lists.

After some work, I wanted to go back to the MainView layout file in order to make some adjustments and there I have my issue:

Caused by: java.lang.ClassNotFoundException: myproject.view.main.activeconversation.ActiveConversationView

Since the GLUON Scene Builder failed to load my layout, I attempted do it with the integrated Scene Builder and it works perfectly.

I whish somebody can help me understand why GLUON's Scene Builder won't open my MainView. I know I can keep using the integrated Scene Builder, but it's slow, and laggy and layout is too constraint.

I even tried to add manually the components's relative FXML file but I got the following error :

Finally, as a last point, each of these components do not contain nested custom controls. So, only the MainView contains the 3 custom controls. And each of these components constructor relies on the same code (e.g. the ConversationView):

public ConversationsView() {
        this.viewModel = new ConversationsViewModel();

        final FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("ConversationsLayout.fxml"));
        fxmlLoader.setClassLoader(getClass().getClassLoader()); // I read on another SO post that this could solve the problem, but it does not.
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);

        try {
            fxmlLoader.load();
        } catch (final IOException ioe) {
            throw new RuntimeException(ioe);
        }
    }

来源:https://stackoverflow.com/questions/52117662/javafx-scenebuilder-classnotfoundexception-when-loading-custom-control

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!