Insert FXML into Scroll Pane and pass data to controller

0

My system consists of a main screen, which has a scroll-pane that I'm populating with a list of other FXML scenes, a code I picked up from an example on the internet, follows the code:

@FXML
private Label label;

@FXML
private VBox pnl_scroll;

@FXML
private void handleButtonAction(MouseEvent event) {
    refreshNodes();
}

@Override
public void initialize(URL url, ResourceBundle rb) {

    refreshNodes();
}

private void refreshNodes() {
    pnl_scroll.getChildren().clear();

    Node[] nodes = new Node[15];

    for (int i = 0; i < 10; i++) {
        try {
            nodes[i] = (Node) FXMLLoader.load(getClass().getResource("Item.fxml"));
            pnl_scroll.getChildren().add(nodes[i]);

        } catch (IOException ex) {
            Logger.getLogger(HomeController.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}

The problem is, I need to set the data for each item.fxml in scroll-pane before adding them, how can I do that?

Thank you!

    
asked by anonymous 29.09.2018 / 15:33

1 answer

0

You can get the controller of these views as follows:

FXMLLoader loader = new FXMLLoader(getClass().getResource("Item.fxml"));
ItemController controller = loader.getController();
Node root = loader.load();

With this you can call their methods through the variable "controller".

Another thing you can do is to use "fx: include" directly in the ".fxml" file. I have tried to insert them through the scene builder but I never could, although after you include them in the file the scene builder already shows your main view with the others included. This way the file would look like this:

<VBox prefHeight="200.0" prefWidth="100.0">
    <children>
        <fx:include fx:id="itemId" source="Item.fxml"/>
    </children>
</VBox>

To access the controller of this view, for example, you only declare a variable with the Node id plus the word "Controller" at the end. It would look like this:

@FXML
private AchorPane itemId;

@FXML
private ItemController itemIdController;

For more information on you can access this link .

    
29.09.2018 / 22:55