Peter Holdensgaard
Peter Holdensgaard

Reputation: 57

Listview delete items when clicked javafx

I have a listview like this

List<SellAbleItems> table1 = Main.readFromTable1();
ListView<SellAbleItems> listOfItems = new ListView<>();
listOfItems.getItems().addAll(table1);

This is good because it goes through my table1 items. The list contains of a name and price. And my question goes on how to delete items with a mouse click on a item and then hit a button that say delete? The problem I see, is that I don't know how many items are available, it depends of how many items the user adds.

Upvotes: 0

Views: 2492

Answers (1)

fabian
fabian

Reputation: 82461

Just remove the selected item:

int index = listOfItems.getSelectionModel().getSelectedIndex();

if (index >= 0) {
    listOfItems.getItems().remove(index);
}

Upvotes: 2

Related Questions