首页 > 解决方案 > SceneBuilder - 添加依赖于第三方组件的自定义组件

问题描述

所以这篇文章:自定义 FXML 组件(带控制器)没有出现在 SceneBuilder 的“导入 jar”对话框中

它说您不能导入依赖第三方组件的自定义组件。但我无法相信这一点(这将是愚蠢的)......我现在正在努力创建一个基于 Library JFoenix组件的自定义组件。

所以我有这个 fxml 和控制器:

可选列表.fxml

<?xml version="1.0" encoding="UTF-8"?>
<?import com.jfoenix.controls.JFXButton?>
<?import com.jfoenix.controls.JFXListView?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<fx:root type="AnchorPane" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1">
   <children>
      <VBox alignment="TOP_CENTER" prefHeight="743.2" prefWidth="400.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
         <children>
            <Label fx:id="titleLabel" text="Title" textFill="#000000de">
               <font>
                  <Font name="Roboto Medium" size="36.0" />
               </font>
               <VBox.margin>
                  <Insets bottom="40.0" top="40.0" />
               </VBox.margin>
            </Label>
            <JFXListView fx:id="itemList" prefHeight="660.0" prefWidth="400.0" />
            <JFXButton fx:id="addButton" focusTraversable="false" onAction="#addElement" prefHeight="50.0" prefWidth="500.0" styleClass="addbutton" stylesheets="@application.css" text="+" textFill="#21ce12ad">
               <font>
                  <Font name="Roboto" size="33.0" />
               </font>
            </JFXButton>
         </children>
      </VBox>
   </children>
</fx:root>

可选列表.java

package de.goehring.components;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXListView;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;

import java.io.IOException;

public class SelectableList<T> extends AnchorPane  {

    @FXML
    private Label titleLabel;

    @FXML
    private JFXListView<T> itemList;

    @FXML
    private JFXButton addButton;

    public SelectableList() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("SelectableList.fxml"));
        fxmlLoader.setRoot(this);
        fxmlLoader.setController(this);
        try {
            fxmlLoader.load();
        } catch (IOException exception) {
            throw new RuntimeException(exception);
        }
    }


    @FXML
    void addElement() {

    }

}

究竟我做错了什么。当作为 jar 导入时,我的 SceneBuilder 无法识别此组件。

标签: javajavafxscenebuilderjfoenix

解决方案


@slaw 发布的相关问题解决了我的问题。

查看它:SceneBuilder 嵌套自定义节点

所以基本上,如果您对这个主题感到震惊,请克隆 SceneBuilder 并通过在套件项目下检查 class 来开始调试它JarExplorer

通过添加堆栈跟踪输出时,x.printStacktrace()您会看到项目未加载的原因。

因此,就我而言,正如链接的帖子所提到的:您需要设置正确的类加载器。(还有一个缺少的斜线。诅咒你的java资源。)

所以这就是我的构造函数现在的样子:

    FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/SelectableList.fxml"));
    fxmlLoader.setRoot(this);
    fxmlLoader.setController(this);
    fxmlLoader.setClassLoader(getClass().getClassLoader());
    try {
        fxmlLoader.load();
    } catch (IOException exception) {
        throw new RuntimeException(exception);
    }

在那之后,它工作得很好。

如果有人在启动 scenebuilder 项目时遇到问题:

使用 IntelliJ。您可以单击 build.gradle 并启动“运行”任务。由于 java 11 需要在命令行中加载模块,因此您可以使用此方法。否则你会得到一个错误,告诉你“JavaFX 组件”丢失。

希望这可以帮助。


推荐阅读