JavaFX:从组中删除文本会导致异常

Moh*_*jar 2 java javafx javafx-2

我把一个ImageView和一个Text放在里面Group.当我尝试删除时Text,我得到以下异常:

Exception in thread "JavaFX Application Thread" java.util.ConcurrentModificationException
    at com.sun.javafx.collections.VetoableListDecorator$VetoableIteratorDecorator.checkForComodification(Unknown Source)
    at com.sun.javafx.collections.VetoableListDecorator$VetoableIteratorDecorator.hasNext(Unknown Source)
    at fjr.tool.TestMake$2$2$1.handle(TestMake.java:67)
    at fjr.tool.TestMake$2$2$1.handle(TestMake.java:1)
    at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(Unknown Source)
    at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(Unknown Source)
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

package fjr.tool;

import java.io.File;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class TestMake extends Application{

    int num  = 2; 
    Group[] groupCollection = new Group[num];
    ImageView view[] = new ImageView[num]; 

    String url[] = new String[]{
            "E:/testimage/1.jpg", 
            "E:/testimage/2.jpg"
    };

    @Override
    public void start(Stage stage) throws Exception {
        Group root = new Group(); 
        stage.setScene(new Scene(root, 500, 400));  

        HBox box = new HBox(){{
            setSpacing(10); 
        }};



        box.getChildren().addAll(new VBox(){{
            setSpacing(5); 
            getChildren().addAll(new  Button("ADD LABEL"){{
                setPrefWidth(100);
                setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {  
                        int yy = 0 ; 
                        for(int i=0; i< num; i++){
                            double width = view[i].getImage().getWidth(); 
                            Text text = new Text("AA"); 
                            text.setTranslateX(width - 20);
                            text.setTranslateY(yy); 
                            groupCollection[i].getChildren().add(text); 
                            yy += view[i].getImage().getHeight(); 
                        }
                    }
                });
            }}, new Button("REMOVE LABEL"){{
                setPrefWidth(100); 
                setOnAction(new EventHandler<ActionEvent>() {
                    @Override
                    public void handle(ActionEvent arg0) {  
                        for(int i=0; i< num;i++){
                            Group g = groupCollection[i]; 
                            for(Node node : g.getChildren()){
                                if(node instanceof Text){
                                    g.getChildren().remove(node); 
                                }
                            }
                        }
                    }
                });
            }}); 
        }}, 

        new ScrollPane(){{
            setPrefSize(370, 300);
            setContent( new VBox(){{
                setSpacing(5);
                for(int i=0; i< num; i++){
                    final int j = i; 
                    groupCollection[i] = new Group(view[i] = new ImageView() {{
                        setImage(new Image(new File(url[j]).toURI().toString()));
                    }}); 
                    getChildren().add(groupCollection[i]); 
                }

            }});
        }}
                ); 

        root.getChildren().add(box); 
        stage.show(); 
    }

    public static void main(String[] args){
        launch(args); 
    }
}
Run Code Online (Sandbox Code Playgroud)

Jam*_*s_D 7

在迭代时,不能直接修改List.迭代器变得混乱.

List<Node> nodesToRemove = new ArrayList<>();
for(Node node : g.getChildren()){
    if(node instanceof Text){
        nodesToRemove.add(node);
    }
}

g.getChildren().removeAll(nodesToRemove);
Run Code Online (Sandbox Code Playgroud)

或更好):

for (Iterator<Node> iterator = g.getChildren().iterator(); iterator.hasNext();) {
    if (iterator.next() instanceof Text) {
        iterator.remove();
    }
}
Run Code Online (Sandbox Code Playgroud)

或者,在Java8中,

g.getChildren().removeIf(node -> node instanceof Text);
Run Code Online (Sandbox Code Playgroud)