Java Gamer
Java Gamer

Reputation: 577

Deleting a selected item on ListView in javafx

I have an ListView 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.

To give you a better understanding. ex:

If I have 5 items in a list and I select and delete "item 2", then item 2 & 3 gets deleted. And items 1, 4 & 5 remains on the list view. If I delete the last item on the list then the item gets deleted and I get a java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Here is my code:

    public void handleDeleteButton() {
    btnDelete.setOnAction(new EventHandler<ActionEvent>() {
        @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);

            }
        }
    });
}

I want only the selected item to be deleted. How do I do that? And how do you make the table multi selectable?

players is the list of players used in the ListView.

Upvotes: 4

Views: 28088

Answers (2)

fabian
fabian

Reputation: 82461

You remove 2 items from the list using the following lines:

playerList.getItems().remove(selectedIdx);
        // ^ this should return players
players.remove(selectedIdx);

Remove one of them.

To allow multiple selection, set MultipleSelectionModel.selectionMode to SelectionMode.MULTIPLE.

Upvotes: 6

a3.14_Infinity
a3.14_Infinity

Reputation: 5843

final int newSelectedIdx =
                        (selectedIdx == playerList.getItems().size() - 1)
                                ? selectedIdx - 1
                                : selectedIdx;

                playerList.getItems().remove(selectedIdx);
  1. As you can see, once newSelectedIdx is assigned, then you try to remove selectedIdx. Sometimes, newSelectedIdx will be same as selectedIdx. Thats the reason why even though you intend to remove one item, other item also gets removed.
  2. Suppose you have only one item left, you will remove that item and then select it, you will get indexOutofBoundsException

What you could do is that deleting logic could be isolated in handleDeleteButton(). The selection event could be handled in some other method, so that you do not mix up delete and select in same function.

Upvotes: 0

Related Questions