Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java getting data from multiple items of ListView

To return the item selected from a ListView in Java, I would use this code:

listview.getSelectionModel().getSelectedItem();

However, if my ListView allows multiple selection, I can't find a direct way to return all of the items selected in the ListView. Is there a straightforward approach to this?

like image 912
user8913 Avatar asked Aug 19 '26 09:08

user8913


1 Answers

There is a getSelectedItems() method of the SelectionModel that should do what you want. It returns an observable list - so you can monitor it for changes with a ListChangedLister.

    ListView<String> listView = new ListView<>();
    ObservableList<String> list = FXCollections.observableArrayList();
    listView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
    listView.setItems(list);
    list.add("Item 1");
    list.add("Item 2");
    list.add("Item 3");

    List<String> selected = listView.getSelectionModel().getSelectedItems();
like image 50
L McClean Avatar answered Aug 21 '26 00:08

L McClean