JavaFX:未在自定义组件上调用 initialize() 方法

JavaFX: initialize() method not being called on custom component

我正在尝试使用 FXML 标记以及扩展 HBox 的控制器来创建我自己的自定义 JavaFX 组件。无论出于何种原因,根本没有调用 initialize() 方法(我看不到任何调试输出)。这只发生在我的自定义组件上,我的所有其他控制器都按预期运行并且始终处于初始化状态。我不知道哪里出了问题 - 这是我的代码。

NotificationItem.fxml(标记)

<fx:root type="javafx.scene.layout.HBox" xmlns:fx="http://javafx.com/fxml" spacing="5">
  <ImageView fx:id="image" preserveRatio="true" fitWidth="60" />
  <VBox alignment="center">
    <Text text="notification" />
    <Label fx:id="title" />
    <Label fx:id="content" />
    <Label fx:id="timestamp" />
  </VBox>
</fx:root>

NotificationItem.java(控制器)

public class NotificationItem extends HBox {
    public static final String FXML_FILENAME = "NotificationItem.fxml";

    @FXML private ResourceBundle resources;  
    @FXML private ImageView image;
    @FXML private Label title;
    @FXML private Label content;
    @FXML private Label timestamp;

    private Notification notification; 
    private AbstractModel associatedModel;

    public NotificationItem(Notification notification) {
        this.notification = notification;
        this.associatedModel = notification.getAssociatedModel();

        FXMLHelper.loadFxml("/com/github/norbo11/topbuilders/fxml/" + FXML_FILENAME, this, this);
    }

    @FXML
    public void initialize() {      
        System.out.println(notification.getType());

        switch (notification.getType()) {
            case ASSIGNMENT_CLOSE_TO_END:
                break;
            case EMPLOYEE_ASSIGNMENT_COMPLETE:
                break;
            case NEW_ASSIGNMENT:
                break;
            case NEW_MESSAGE:
                Message message = (Message) associatedModel;
                title.setText(resources.getString("home.notifications.new_message"));
                content.setText(resources.getString("messages.sender") + ": " + message.getSender());
                break;
            case NEW_QUOTE_REQUEST:
                break;
        }

        timestamp.setText(Util.formatDate(notification.getDate()));
    }
}

用于加载我的 FXML 的程序(忽略 return 值,在本例中未使用)

public static LoadedFXML loadFxml(String filename, Object root, Object controller) {
        Log.info("Loading FXML: " + filename);
        Parent loadedRoot = null;
        AbstractController loadedController = null;

        try {
            FXMLLoader loader = new FXMLLoader(Main.getApp().getClass().getResource(filename));
            if (root != null) loader.setRoot(root);
            if (controller != null) loader.setController(controller);

            if (!filename.equals(LoginScene.getAbsoluteFxmlFilename())) {
                Employee user = Employee.getCurrentEmployee();

                //If the user is logged in
                if (user != null) {
                    Locale locale = Employee.getCurrentEmployee().getSettings().getLocale();
                    loader.setResources(ResourceBundle.getBundle("lang.lang", locale, ClassLoader.getSystemClassLoader()));
                }
            }

            if (root == null) loadedRoot = loader.load();
            if (controller == null) loadedController = loader.getController();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new LoadedFXML(loadedRoot, loadedController);
    }

我可能弄错了,因为我无法测试你代码的大部分,但我认为问题出在这里:

public static LoadedFXML loadFxml(String filename, Object root, Object controller) {

    if (root == null) loadedRoot = loader.load();
}

由于您是从构造函数调用的:

public NotificationItem(Notification notification) {
    FXMLHelper.loadFxml(FXML_FILENAME, this, this);
}

root 不会为空,您也不会加载 FXML 文件。其实你前面几行设置root是相反的

只要改变条件,initialize就会被调用:

if (root != null) loadedRoot = loader.load();