Hy guys, I'm developing an application using javafx and fxml. What I'm currently trying to do is to update a flowpane, adding components to it when the user performs an action (is not the case but let's take as an example the click of a button).
Let's suppose that the method just has to add buttons with the passed labels to the flowpane.
In the Controller of the fxml that contains the flowpane I have this:
public class CategoryPaneController implements Initializable {
@FXML
private FlowPane categoryContainer;
public void setCategories(String[] labels) throws IOException{
for(String label : labels){
Button button = new Button(label);
categoryContainer.getChildren().add(button);
}
}
}
This method is called in another controller as follows:
public class AddCategoryController implements Initializable {
@FXML
private Pane addCategoryPane;
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
@FXML
private void refreshCategories(ActionEvent event) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("/view/categoryPane.fxml"));
loader.load();
CategoryPaneController cat = loader.getController();
String[] labels = {"categoria3", "categoria4"};
cat.setCategories(labels);
}
}
The categoryPane.fxml is the following:
<ScrollPane fx:id="storeMainPane" fx:controller="controller.CategoryPaneController">
<content>
<VBox prefHeight="629.0" prefWidth="862.0">
<children>
<Label alignment="TOP_CENTER" contentDisplay="CENTER" prefHeight="68.0" prefWidth="857.0" text="Magazzino" />
<FlowPane fx:id="categoryContainer" prefHeight="549.0" prefWidth="862.0" />
</children>
</VBox>
</content>
</ScrollPane>
And the following is the addCategory.fxml file
<Pane fx:id="addCategoryPane" fx:controller="controller.AddCategoryController">
<children>
<Button onAction="#refreshCategories" text="aggiungi categoria" />
</children>
</Pane>
I've debbugged the code and the method is called in the right way, it adds the buttons to the flowpane but the latter doesn't change.
Thanks in advance. <3