Reputation: 329
I am new to java, so please take it easy on me. I am trying to dynamically populate a xml file named clients.xml with clients. Each client needs to be displayed individually inside a VBox.
The xml for creating a individual client is as follows:
<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>
What I would like to do is dynamically create the above clientBox inside a HBox with the fx:id of clientSection via a loop in my clients controller.
Basically what I am trying to do is in ClientsCotroller, do something like:
for (Client client : Bank.getInstance().getClients()) {
clientSection.getChildren().add(new clientBox() );
}
Please could I be advised as to how I could possibly achieve this. Kind regards, Matt.
Upvotes: 0
Views: 733
Reputation: 329
So all I ended up doing was creating a custom control. I separated the xml I wanted to create dynamically into its own fxml file named listItem.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>
I then created a separate controller for this file named ListItemControl.java.
ListItem.java
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);
}
}
}
after that every time I wanted to use the custom component, I would create a new one like this:
ListItemControl clientBoxControl = new ListItemControl();
Upvotes: 2
Reputation: 1691
You can use JAXB as xml to Java objects convertor. Following to these examples : https://dzone.com/articles/using-jaxb-for-xml-with-java try to marshal/unmarshal the elements.
Upvotes: 0