Mam dwa obiekty ListView
s, allStudentsList
, które już są w nim zapełnione, currentStudentList
nie ma żadnych. Moim celem, gdy użytkownik wybierze element w allStudentList
, jest przeniesienie tego elementu do currentStudentList
. Zrobiłem to poprzez umieszczenie słuchacza w modelu wyboru allStudentList
.Jak przenosić elementy między dwoma ListViews z przesłuchiwaczem zmian JavaFX
Otrzymuję IndexOutOfBoundsException
i nie jestem pewien, dlaczego tak się dzieje. Z testowania wydaje się, że problem ten jest izolowany do ostatnich 4 linii tej metody, ale nie jestem pewien dlaczego.
allStudentsList.getSelectionModel().selectedItemProperty()
.addListener((observableValue, oldValue, newValue) -> {
if (allStudentsList.getSelectionModel().getSelectedItem() != null) {
ArrayList<String> tempCurrent = new ArrayList<>();
for (String s : currentStudentList.getItems()) {
tempCurrent.add(s);
}
ArrayList<String> tempAll = new ArrayList<>();
for (String s : allStudentsList.getItems()) {
tempAll.add(s);
}
tempAll.remove(newValue);
tempCurrent.add(newValue);
// clears current studentlist and adds the new list
if (currentStudentList.getItems().size() != 0) {
currentStudentList.getItems().clear();
}
currentStudentList.getItems().addAll(tempCurrent);
// clears the allStudentList and adds the new list
if (allStudentsList.getItems().size() != 0) {
allStudentsList.getItems().clear();
}
allStudentsList.getItems().addAll(tempAll);
}
});
Która linia rzuca wyjątek? –
allStudents.getItems(). Clear(); – Philayyy
allStudentsList.getItems(). AddAll (tempAll); – Philayyy