I am having simple vbox which will have six hboxes and I can delete one/all and add once again.
My code :
@FXML
private VBox userSelectedValues;
int index = userSelectedValues.getChildren().indexOf(event.getSource());
userSelectedValues.getChildren().remove(cnt);
ObservableList<Node> hboxNodes = FXCollections.observableArrayList();
hboxNodes.addAll(userSelectedValues.getChildren());
userSelectedValues.getChildren().clear();
userSelectedValues.getChildren().addAll(hboxNodes);
Scenario : there are six elements initially
1) I deleted 4th element - works fine - now total 5 elements
2) I deleted 3rd element - works fine - now total 4 elements
3) Now if I want to delete 6th element is giving me the indexOutOfBoutnd which is expected.
So how to regenerate or refresh indexes.
Thank you,
What do you mean with "refresh" indexes? The children are in an ObservableList. If you remove an item, then the indexes change.
Example:
The 2nd item is removed, the console output is as expected:
According to the documentation about indexOf: