title: ListView - removeAll doesn't work?
body: <p>(This is code from the book "JavaFX 2.0 by example" by Carl Dea - the code example is freely available at Apress so I'm sure they don't mind me using it here)</p>
  
  <p>I have example code which works perfectly</p>
  
  <pre><code>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&lt;String&gt; candidates = FXCollections.observableArrayList("Superman",
                  "Spiderman",
                  "Wolverine",
                  "Police", 
                  "Fire Rescue", 
                  "Soldiers", 
                  "Dad &amp; Mom", 
                  "Doctor", 
                  "Politician", 
                  "Pastor", 
                  "Teacher");
          final ListView&lt;String&gt; candidatesListView = new ListView&lt;String&gt;(candidates);
          candidatesListView.setPrefWidth(150);
          candidatesListView.setPrefHeight(150);
  
          gridpane.add(candidatesListView, 0, 1);
  
          // heros
          final ObservableList&lt;String&gt; heroes = FXCollections.observableArrayList();
          final ListView&lt;String&gt; heroListView = new ListView&lt;String&gt;(heroes);
          heroListView.setPrefWidth(150);
          heroListView.setPrefHeight(150);
  
          gridpane.add(heroListView, 2, 1);
  
  
          // select heroes
          Button sendRightButton = new Button("&gt;");
          sendRightButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
              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("&lt;");
          sendLeftButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
              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();
      }
  }
  </code></pre>
  
  <p>It is code for moving persons back and forth between two listviews, one at a time. What I want to do is make it possible to select and move several persons in one click.</p>
  
  <p>The relevant excerpts I want to change are:</p>
  
  <pre><code>final ListView&lt;String&gt; candidatesListView = new ListView&lt;String&gt;(candidates);
      candidatesListView.setPrefWidth(150);
      candidatesListView.setPrefHeight(150);
  
      gridpane.add(candidatesListView, 0, 1);
  
      // heros
      final ObservableList&lt;String&gt; heroes = FXCollections.observableArrayList();
      final ListView&lt;String&gt; heroListView = new ListView&lt;String&gt;(heroes);
  </code></pre>
  
  <p>...</p>
  
  <pre><code>// select heroes
      Button sendRightButton = new Button("&gt;");
      sendRightButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
          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("&lt;");
      sendLeftButton.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
  
          public void handle(ActionEvent event) {
              String notHero = heroListView.getSelectionModel().getSelectedItem();
              if (notHero != null) {
                  heroListView.getSelectionModel().clearSelection();
                  heroes.remove(notHero);
                  candidates.add(notHero);
              }
          }
      });
  </code></pre>
  
  <p>What I have tried changing:</p>
  
  <p>First I add the following import:</p>
  
  <pre><code>import javafx.scene.control.SelectionMode;
  </code></pre>
  
  <p>Then I add the lines</p>
  
  <pre><code>candidatesListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  heroListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  </code></pre>
  
  <p>beneath the respective declarations of the two lists.</p>
  
  <p>Lastly I change the code of the handling of the eventbutton to </p>
  
  <pre><code>public void handle(ActionEvent event) {
              ObservableList&lt;String&gt; potential = candidatesListView.getSelectionModel().getSelectedItems();
              if (potential != null) {
  
                  System.out.println(potential);
                  candidates.removeAll(potential);
                  heroes.addAll(potential);
                  candidatesListView.getSelectionModel().clearSelection();
              }
          }
  </code></pre>
  
  <p>That is- I change it to getSelectedItem_s_, then I addAll and removeAll instead of merely adding/removing one person. This just leaves the listView blank when I try to move several people over. What gives?</p>
  
  <p>Ps. I also tried just adding/removing several people one at a time by iterating over the list "potential", but that also gave the wrong result.</p>