[英]Scene builder custom components being expanded in fxml
我已經使用JavaFX幾個月了,我必須說我真的很喜歡它。 但是,最近,我一直在嘗試使用場景構建器制作自定義組件。
可以說我有一個名為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導入Scene生成器,它可以像常規組件一樣使用它。 因此,如果我創建一個新視圖並將其拖動到該視圖上,則會得到類似的內容。
<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>
有誰知道如何將自定義組件放到場景生成器中進行擴展,或者這是尚未修復/實現的錯誤/功能? 我覺得這對於制作可重用的自定義組件至關重要。
經過調查后,我意識到了我的問題。 我是直接導入FXML,這當然會使FXML得到擴展。 正確的解決方案是將類和FXML一起JAR並導入該jar文件。 這將產生正確的行為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.