提问者:小点点

在fxml中扩展的场景构建器自定义组件


我已经使用JavaFX几个月了,我必须说我真的很喜欢它。但是,最近我一直在尝试用scene builder制作自定义组件。

假设我有一个名为CustomTable的自定义组件,它看起来像这样

public class CustomTable extends VBox{

public CustomTable(){
    try {
        FXMLLoader loader = new FXMLLoader(getClass().getResource("CustomTable.fxml"));
        loader.setRoot(this);
        loader.setController(this);
        loader.load();
    } catch (IOException e ){
        throw new RuntimeException(e);
    }
}

}

我有相应的FXML文件

<fx:root prefHeight="444.0" prefWidth="472.0" type="VBox" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="CustomTable">
<children>
    <TableView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="410.0" prefWidth="297.0" VBox.vgrow="ALWAYS">
        <columns>
            <TableColumn prefWidth="75.0" text="Testing Column 1" />
            <TableColumn prefWidth="75.0" text="Testing Column 2" />
        </columns>
        <columnResizePolicy>
            <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
        </columnResizePolicy>
    </TableView>
</children>

现在,如果我将这个CustomTable导入到场景生成器中,它允许我像常规组件一样使用它。所以,如果我创建一个新视图并将其拖到该视图上,我会得到类似的结果。

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="436.0" prefWidth="621.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
        prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
        prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0"
        vgrow="SOMETIMES" />
</rowConstraints>
<children>
    <VBox prefHeight="444.0" prefWidth="472.0" GridPane.columnIndex="1">
        <children>
            <TableView maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308"
                minHeight="-Infinity" minWidth="-Infinity" prefHeight="410.0"
                prefWidth="297.0" VBox.vgrow="ALWAYS">
                <columns>
                    <TableColumn prefWidth="75.0" text="Testing Column 1" />
                    <TableColumn prefWidth="75.0" text="Testing Column 2" />
                </columns>
                <columnResizePolicy>
                    <TableView fx:constant="CONSTRAINED_RESIZE_POLICY" />
                </columnResizePolicy>
            </TableView>
        </children>
    </VBox>
    <Button mnemonicParsing="false" prefHeight="38.0" prefWidth="94.0"
        text="TEST" GridPane.halignment="CENTER" />
</children>

我认为将组件扩展出去是没有意义的。我觉得应该是这样的。

<GridPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
minWidth="-Infinity" prefHeight="436.0" prefWidth="621.0"
xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8">
<columnConstraints>
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
        prefWidth="100.0" />
    <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0"
        prefWidth="100.0" />
</columnConstraints>
<rowConstraints>
    <RowConstraints minHeight="10.0" prefHeight="30.0"
        vgrow="SOMETIMES" />
</rowConstraints>
<children>
    <CustomTable GridPane.columnIndex="1" prefHeigh="444.0"
        prefWidth="472.0" />
    <Button mnemonicParsing="false" prefHeight="38.0" prefWidth="94.0"
        text="TEST" GridPane.halignment="CENTER" />
</children>

有谁知道如何使自定义组件在放入场景构建器时从扩展,或者这是一个尚未修复/实现的错误/功能?我觉得这对于制作可重用的自定义组件非常重要。


共1个答案

匿名用户

经过一些调查,我意识到了我的问题。我直接导入FXML,这当然是FXML扩展是有意义的。正确的解决方案是将类和 FXML 一起 JAR 并导入该 jar 文件。这将产生正确的行为。