0

I am having a problem with this piece of code:

for(Node n : start.getChildren()) {
                    singlewidth = ((CustomController) n).getWidth();
                    totalwidth += singlewidth;
                if(swidth < totalwidth) {
                    //start.getChildren().remove(b);
                    start.getChildren().add(mb);
                    start.getChildren().remove(n);
                }
                if(swidth > totalwidth){
                    start.getChildren().add(n);
                    start.getChildren().remove(mb);
                }
            }

The trick here is that I want to go through all the elements in the HBox, and try to adapt them by width. But on the line, where I re-add the Node n, NetBeans throws the ConcurrentModificationException. Can somebody help me please?

  • What I do sometimes is create a `List` and add the `Nodes` that will be removed. Then after the loop ends you can remove all the nodes in the list. The other option is to iterate the list. – SedJ601 Aug 01 '18 at 16:38
  • Can you please explain how can I use the list iterator in this scenario? – Marko Watzak Aug 01 '18 at 19:00
  • There are several examples in the link provided. – SedJ601 Aug 01 '18 at 19:03
  • BTW: If `start.getChildren().add(n);` is executed, javafx throws an exception since `n` is already a child of `start`. Executing `start.getChildren().add(mb);` more than once results in a exception for the same reason... – fabian Aug 01 '18 at 22:17
  • Okay, thank you for the clarification of the problem. I think I'll be able to sort it now. – Marko Watzak Aug 02 '18 at 10:53

0 Answers0