如何在java中动态创建自定义xml元素(或自定义javafx场景控件)
我是java新手,所以请对我放轻松。我正在尝试用客户机动态填充名为clients.xml的xml文件。每个客户端都需要在VBox中单独显示 用于创建单个客户端的xml如下所示:如何在java中动态创建自定义xml元素(或自定义javafx场景控件),java,xml,Java,Xml,我是java新手,所以请对我放轻松。我正在尝试用客户机动态填充名为clients.xml的xml文件。每个客户端都需要在VBox中单独显示 用于创建单个客户端的xml如下所示: <HBox fx:id="clientBox" alignment="CENTER_LEFT" prefHeight="52.0" prefWidth="194.0"> <children>
<HBox fx:id="clientBox" alignment="CENTER_LEFT" prefHeight="52.0" prefWidth="194.0">
<children>
<ImageView fitHeight="35.0" fitWidth="35.0" pickOnBounds="true" preserveRatio="true">
<image>
<Image url="@../assets/images/user-icon.png" />
</image>
</ImageView>
<VBox>
<children>
<HBox>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="Name:">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" styleClass="fn-14" text="Liam Smith" />
</children>
</HBox>
<HBox prefHeight="10.0" />
<HBox>
<children>
<Text strokeType="OUTSIDE" strokeWidth="0.0" text="ID:">
<HBox.margin>
<Insets right="10.0" />
</HBox.margin>
</Text>
<Text strokeType="OUTSIDE" strokeWidth="0.0" styleClass="fn-14" text="968745632952" />
</children>
</HBox>
</children>
<padding>
<Insets left="5.0" right="5.0" />
</padding>
</VBox>
<VBox alignment="CENTER" HBox.hgrow="ALWAYS">
<children>
<Button mnemonicParsing="false" text="Delete">
<styleClass>
<String fx:value="background-transparent" />
<String fx:value="fn-14" />
<String fx:value="fill-red" />
</styleClass>
<VBox.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</VBox.margin>
</Button>
<Button mnemonicParsing="false" text="Edit">
<styleClass>
<String fx:value="background-transparent" />
<String fx:value="fn-14" />
<String fx:value="fill-green" />
</styleClass>
<VBox.margin>
<Insets bottom="2.0" left="2.0" right="2.0" top="2.0" />
</VBox.margin>
</Button>
</children>
</VBox>
</children>
<padding>
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding>
<styleClass>
<String fx:value="background-light-grey" />
<String fx:value="hover-hand" />
<String fx:value="hover-light-blue" />
</styleClass>
<VBox.margin>
<Insets bottom="5.0" top="5.0" />
</VBox.margin>
</HBox>
请告诉我如何才能做到这一点。
你好,Matt。您可以使用JAXB作为xml到Java对象转换器。下面是这些示例:尝试封送/取消封送元素 所以我最后做的就是创建一个自定义控件。我将要动态创建的xml分离到它自己的名为listItem.fxml的fxml文件中 listItem.fxml
<fx:root alignment="CENTER_LEFT" prefHeight="78.0" prefWidth="363.0" stylesheets="@../css/styles.css" type="javafx.scene.layout.HBox" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1">
<!-- xml for listitem comes here -->
</fx:root>
之后,每次我想使用自定义组件时,我都会创建一个新组件,如下所示:
ListItemControl clientBoxControl = new ListItemControl();
如果您说明使用哪个XML库,您可能会得到更好的帮助。
public class ListItemControl extends HBox {
// associate the control to the fxml
public ListItemControl() {
FXMLLoader fxmlLoader = new
FXMLLoader(getClass().getResource("/com/company/fxml/listItem.fxml"));
fxmlLoader.setRoot(this);
fxmlLoader.setController(this);
try {
fxmlLoader.load();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
ListItemControl clientBoxControl = new ListItemControl();