Java ListView-removeAll不';不行?

Java ListView-removeAll不';不行?,java,javafx-2,Java,Javafx 2,(这是Carl Dea的《JavaFX 2.0示例》一书中的代码-该代码示例在Apress上免费提供,因此我相信他们不会介意我在这里使用它) 我有很好的示例代码 package javafx2introbyexample.chapter1.recipe1_11; import javafx.application.Application; import javafx.collections.FXCollections; import javafx.collections.ObservableL

(这是Carl Dea的《JavaFX 2.0示例》一书中的代码-该代码示例在Apress上免费提供,因此我相信他们不会介意我在这里使用它)

我有很好的示例代码

package javafx2introbyexample.chapter1.recipe1_11;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.HPos;
import javafx.geometry.Insets;
import javafx.geometry.VPos;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

/**
 *
 * @author cdea
 */
public class CreatingAndWorkingWithObservableLists extends Application {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Chapter 1-11 Creating and Working with ObservableLists");
        Group root = new Group();
        Scene scene = new Scene(root, 400, 250, Color.WHITE);

        // create a grid pane
        GridPane gridpane = new GridPane();
        gridpane.setPadding(new Insets(5));
        gridpane.setHgap(10);
        gridpane.setVgap(10);

        // candidates label
        Label candidatesLbl = new Label("Candidates");
        GridPane.setHalignment(candidatesLbl, HPos.CENTER);
        gridpane.add(candidatesLbl, 0, 0);

        Label heroesLbl = new Label("Heroes");
        gridpane.add(heroesLbl, 2, 0);
        GridPane.setHalignment(heroesLbl, HPos.CENTER);

        // candidates
        final ObservableList<String> candidates = FXCollections.observableArrayList("Superman",
                "Spiderman",
                "Wolverine",
                "Police", 
                "Fire Rescue", 
                "Soldiers", 
                "Dad & Mom", 
                "Doctor", 
                "Politician", 
                "Pastor", 
                "Teacher");
        final ListView<String> candidatesListView = new ListView<String>(candidates);
        candidatesListView.setPrefWidth(150);
        candidatesListView.setPrefHeight(150);

        gridpane.add(candidatesListView, 0, 1);

        // heros
        final ObservableList<String> heroes = FXCollections.observableArrayList();
        final ListView<String> heroListView = new ListView<String>(heroes);
        heroListView.setPrefWidth(150);
        heroListView.setPrefHeight(150);

        gridpane.add(heroListView, 2, 1);


        // select heroes
        Button sendRightButton = new Button(">");
        sendRightButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                String potential = candidatesListView.getSelectionModel().getSelectedItem();
                if (potential != null) {
                    candidatesListView.getSelectionModel().clearSelection();
                    candidates.remove(potential);
                    heroes.add(potential);
                }
            }
        });

        // deselect heroes
        Button sendLeftButton = new Button("<");
        sendLeftButton.setOnAction(new EventHandler<ActionEvent>() {

            public void handle(ActionEvent event) {
                String notHero = heroListView.getSelectionModel().getSelectedItem();
                if (notHero != null) {
                    heroListView.getSelectionModel().clearSelection();
                    heroes.remove(notHero);
                    candidates.add(notHero);
                }
            }
        });

        VBox vbox = new VBox(5);
        vbox.getChildren().addAll(sendRightButton,sendLeftButton);

        gridpane.add(vbox, 1, 1);
        GridPane.setConstraints(vbox, 1, 1, 1, 2,HPos.CENTER, VPos.CENTER);

        root.getChildren().add(gridpane);        
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}
然后我添加行

candidatesListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
heroListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
在两个列表各自的声明下面

最后,我将eventbutton的处理代码更改为

public void handle(ActionEvent event) {
            ObservableList<String> potential = candidatesListView.getSelectionModel().getSelectedItems();
            if (potential != null) {

                System.out.println(potential);
                candidates.removeAll(potential);
                heroes.addAll(potential);
                candidatesListView.getSelectionModel().clearSelection();
            }
        }
公共无效句柄(ActionEvent事件){
ObservableList potential=CandidateListView.getSelectionModel().getSelectedItems();
如果(电位!=null){
系统输出打印LN(电位);
候选人。removeAll(潜在);
英雄。addAll(潜力);
CandidateListView.getSelectionModel().clearSelection();
}
}
也就是说,我将其更改为getSelectedItem\u s\u,然后我添加全部并移除全部,而不仅仅是添加/删除一个人。当我试图移动几个人时,这只会使listView为空。有什么好处


顺便说一句,我还试着通过迭代“潜在”列表一次添加/删除几个人,但这也给出了错误的结果。

不幸的是,您遇到了一个错误:

原始问题是下一个:
ListView.getSelectionMode()
返回其可观察列表的一部分,但不返回副本。因此,从该列表中删除会导致各种问题

在从列表中删除项目之前,使用复制列表的下一个代码:

    sendRightButton.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            ObservableList<String> potential = 
                FXCollections.observableArrayList( //copy
                    candidatesListView.getSelectionModel().getSelectedItems());
            if (potential != null) {
                heroes.addAll(potential);
                candidates.removeAll(potential);
                candidatesListView.getSelectionModel().clearSelection();
            }
        }
    });
sendRightButton.setOnAction(新的EventHandler(){
公共无效句柄(ActionEvent事件){
可观测列表电位=
FXCollections.observableArrayList(//复制
CandidateListView.getSelectionModel().getSelectedItems());
如果(电位!=null){
英雄。addAll(潜力);
候选人。removeAll(潜在);
CandidateListView.getSelectionModel().clearSelection();
}
}
});

这正是我想听到的——也就是说,这不是我的错:)但有没有办法一次性添加和删除多个项目?如果没有,我会张贴如果我找到一个。忍者编辑:哇,太快了。赞美你的名字,我一直在为这件事发愁!
candidatesListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
heroListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
public void handle(ActionEvent event) {
            ObservableList<String> potential = candidatesListView.getSelectionModel().getSelectedItems();
            if (potential != null) {

                System.out.println(potential);
                candidates.removeAll(potential);
                heroes.addAll(potential);
                candidatesListView.getSelectionModel().clearSelection();
            }
        }
    sendRightButton.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            ObservableList<String> potential = 
                FXCollections.observableArrayList( //copy
                    candidatesListView.getSelectionModel().getSelectedItems());
            if (potential != null) {
                heroes.addAll(potential);
                candidates.removeAll(potential);
                candidatesListView.getSelectionModel().clearSelection();
            }
        }
    });