title: Deleting items other than the selected ones in listview in javafx
body: <p>I have been trying to delete all items except the selected items in listview in javafx. I have written the following code. It works sometimes and sometimes does not. I am not able to figure out my mistake. Any help would be appreciated !!</p>
  
  <pre><code>try {
      int i = 0;
      while (!listName.getItems().equals(listName.getSelectionModel().getSelectedItems())) {
          if (listName.getSelectionModel().isSelected(i) == false) {
              listName.getItems().remove(i);
              i--;
          }
          i++;
      }
  } catch (Exception e) {
      e.printStackTrace();
  }
  </code></pre>