title: Deleting a selected item on ListView in javafx
body: <p>I have an <code>ListView</code> with items, and developed a delete function which deletes the item. The problem Im facing is when I delete an item, the item below gets deleted as well.</p>
  
  <p>To give you a better understanding. ex:</p>
  
  <p>If I have 5 items in a list and I select and delete "item 2", then item 2 &amp; 3 gets deleted. And items 1, 4 &amp; 5 remains on the list view. If I delete the last item on the list then the item gets deleted and I get a <code>java.lang.IndexOutOfBoundsException: Index: 0, Size: 0</code> </p>
  
  <p>Here is my code:</p>
  
  <pre><code>    public void handleDeleteButton() {
      btnDelete.setOnAction(new EventHandler&lt;ActionEvent&gt;() {
          @Override
          public void handle(ActionEvent event) {
              final int selectedIdx = playerList.getSelectionModel().getSelectedIndex();
              if (selectedIdx != -1) {
                  String itemToRemove = playerList.getSelectionModel().getSelectedItem();
  
                  final int newSelectedIdx =
                          (selectedIdx == playerList.getItems().size() - 1)
                                  ? selectedIdx - 1
                                  : selectedIdx;
  
                  playerList.getItems().remove(selectedIdx);
                  playerList.getSelectionModel().select(newSelectedIdx);
                  //removes the player for the array
                  System.out.println("selectIdx: " + selectedIdx);
                  System.out.println("item: " + itemToRemove);
                  players.remove(selectedIdx);
  
              }
          }
      });
  }
  </code></pre>
  
  <p>I want only the selected item to be deleted. How do I do that? And how do you make the table multi selectable?</p>
  
  <p><code>players</code> is the list of players used in the <code>ListView</code>.</p>