Brian Stallter
Brian Stallter

Reputation: 159

How to remove individual nodes within groups on a pane javafx

I have run into a roadblock in my programming. I have an array of groups of arcs on a pane (HE.getDatabase()) is an array of groups of arcs. I am trying to loop through them and remove any arc that has a fill of BLACK. If I try to make them green it works and every single Arc that has a fill of BLACK will go to green using the same loop. LIke this:

main.getDeleteSelected().setOnAction(b -> {
    for( int i=0; i < HE.getDatabase().size(); i++){
        for(Node one: HE.getDatabase().get(i).getChildren()){
            if (((Arc) one).getFill() == Color.BLACK){
                ((Arc) one).setFill(Color.GREEN);
            }
        }

    }
});

But if I try to remove the Arc it will only remove one or two on the specified action at a time. Like this:

main.getDeleteSelected().setOnAction(b -> {
    for( int i=0; i < HE.getDatabase().size(); i++){
        for(Node one: HE.getDatabase().get(i).getChildren())        {
            if (((Arc) one).getFill() == Color.BLACK){
                HE.getDatabase().get(i).getChildren().remove(one);
            }
        }

    }
});

I have to keep hitting the menu item several times to get rid of all of the BLACK filled arcs. I'm pretty sure it has to do with the group.getChildren().remove(selected) portion of the code. Any help on this would be greatly appreciated. Is there another way to get rid of a node without referencing the parent?

Upvotes: 0

Views: 849

Answers (2)

James_D
James_D

Reputation: 209388

You can do

for (Group g : HE.getDatabase()) {
    g.getChildren().removeIf(node -> ((Arc)node).getFill() == Color.BLACK);
}

Upvotes: 1

Brian Stallter
Brian Stallter

Reputation: 159

Answer is here. You cannot loop over a list that you are deleting from. How Iterator's remove method actually remove an object

Upvotes: 0

Related Questions