自定义FXML组件(带控制器)没有出现在SceneBuilder的“导入jar”中。对话 [英] Custom FXML component (w/ controller) doesn't appear in SceneBuilder's "Import jar" dialog
问题描述
我正在关注这个在创建自定义组件并导入它时。但是,在单击jar文件后进入导入对话框时,它不会出现。当我注释掉用于构造函数的教程的代码时,它再次出现。但是,我用于制作自定义子组件的子组件都没有出现。为什么以及如何修复它?
I am following this in creating a custom component and importing it. However, when it gets to the Import Dialog after clicking the jar file, it does not appear. When I comment out the code the tutorial used for the constructor, it appears again. However, none of the subcomponents I used to make the custom one appear. Why and how do I fix it?
另外,我使用的是VBox而不是AnchorPane(如教程中所示)。
Also, I am using a VBox instead of an AnchorPane (as seen in the tutorial) for my own thing.
教程中的构造函数代码:
Code of constructor as seen in tutorial:
public CommodityImageLabel() {
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("/fxml/CommodityImageLabel.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
我自己的示例构造函数的代码:
Code for my own example's constructor:
public While() {
FXMLLoader fxmlLoader = new FXMLLoader(
getClass().getResource("BlocksFXML/While.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException exception) {
throw new RuntimeException(exception);
}
}
编辑1:我尝试评论部分代码,当我注释掉try-catch部分时,它会使组件出现在对话框中,但它仍然不显示子组件。
I tried commenting out parts of the code, and when I comment out the try-catch part, it makes the component appear in the dialog, but it still doesn't show the subcomponents.
编辑2:自定义component基本上是一个包含带有Label和TextField的Hbox的VBox。 这里是它应该看起来像什么,最终看起来像成功导入没有try-catch部分。
Edit 2: The custom component is basically a VBox containing an Hbox with a Label and a TextField. Here is what it's supposed to look like vs what ends up looking like when it successfully imports without the try-catch part.
推荐答案
我使用此处的信息解决了这个问题:
I solved this using the information here:
http://www.cuchazinteractive.com/blog/custom- javafx-controls-and-scene-builder
总结:
- 创建FXML文件和扩展根节点的Java类(我给它们命名相同)
- 将FXML文件更改为
fx:root
作为基本节点。 (没有这个就无法工作) - 删除
fx:controller
属性(如果没有这个属性将不起作用) - 编译并将jar添加到场景构建器
- Create an FXML file and a Java class that extends the root node (I gave them the same name)
- Change the FXML file to have
fx:root
as the base node. (will not work without this) - Remove the
fx:controller
attribute (will not work without this) - Compile and add the jar to scene builder
但是,我发现如果你的自定义控件依赖于来自的控件其他库,即使在场景构建器中加载了其他库,也会失败。
However, I have found that if your custom control depends on controls from other libraries, it will fail even if the other library is loaded in scene builder.
以下是最低工作示例
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<fx:root id="AnchorPane" prefHeight="73.0" prefWidth="112.0" type="AnchorPane" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8.0.111">
<children>
<Button layoutX="25.0" layoutY="28.0" mnemonicParsing="false" text="Button" />
</children>
</fx:root>
Java:
package my.amazing.controls;
import java.io.IOException;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.layout.AnchorPane;
public class TestControl extends AnchorPane {
public TestControl() throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("TestControl.fxml"));
loader.setRoot(this);
loader.setController(this);
loader.load();
}
@FXML
public void initialize() {
}
}
这篇关于自定义FXML组件(带控制器)没有出现在SceneBuilder的“导入jar”中。对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!