Reputation: 15
I have a main gui in fxml file created with SceneBuilder and its related controller. In that main gui I've a gridpane where I want to put, for each cell, three child (a label, a TextView and a CheckBox). So I've created an additional fxml with an HBox as root and the three childs.
Now... How could I add by code in the gridpane of the main gui the defined component for each cell and interacts with them?
I mean... what I want to do is something like this in the main gui controller:
for (int i) for (int j) gridpane.add("the_composed_view_in_the_other_fxml", i, j)
Upvotes: 0
Views: 172
Reputation: 209359
If I understand your question correctly, you would do something like this in the initialize
method of your "main" controller:
public class MainController {
@FXML
private GridPane gridpane ;
public void initialize() throws IOException {
int numCols = ... ;
int numRows = ... ;
for (int rowIndex = 0 ; rowIndex < numRows ; rowIndex++) {
for (int colIndex = 0 ; colIndex < numCols ; colIndex++) {
FXMLLoader loader = new FXMLLoader(getClass().getResource("path/to/addtional/fxml"));
gridpane.add(loader.load(), colIndex, rowIndex);
}
}
}
}
For "interacting" with the components loaded from the additional fxml file, it is really the responsibility of the controller for the additional fxml. You can get a reference to each of those controllers after you load the fxml file:
gridpane.add(loader.load(), colIndex, rowIndex);
AdditionalController controller = loader.getController();
and then you can call methods that you have defined in that controller class. You haven't really provided enough detail about what you might want to do here, but, e.g.:
public class AdditionalController {
@FXML
private CheckBox checkBox ;
public BooleanProperty selectedProperty() {
return checkBox.selectedProperty();
}
// etc...
}
and then something like
gridpane.add(loader.load(), colIndex, rowIndex);
AdditionalController controller = loader.getController();
String s = String.format("Item [%d, %d]", colIndex, rowIndex);
controller.selectedProperty().addListener((obs, wasSelected, isNowSelected) -> {
if (isNowSelected) {
// process selection...
System.out.println(s + " is selected");
}
});
Upvotes: 1