I have a following issue. I have a CombBox
which should display names of my Quiz
class (this class has furthemore a list of Tasks
) and I want to remove every single Quiz
from the available selection. I also have the Quizzes
separated by the Teacher
who created them, and I only want to display the according Quizzes
to the selected owner in the 2nd ComboBox
(I had no issues with this part so far).
Here's my code.
QuizComboBox
public class QuizComboBox extends ComboBox {
public QuizComboBox() {
List<Quiz> quizList = new QuizDao().getAllQuizes();
startTask(quizList);
Platform.runLater(() -> {
setCellFactory(new Callback<ListView<Quiz>, ListCell<Quiz>>() {
@Override
public ListCell<Quiz> call(ListView<Quiz> arg0) {
return new ListCell<Quiz>() {
@Override
protected void updateItem(Quiz item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
String quizText = item.getName();
setText(quizText);
}
}
};
}
});
});
}
public void startTask(List<Quiz> quizList){
Runnable task = () -> removeEmptyQuizzes(quizList);
Thread backgroundThread = new Thread(task);
backgroundThread.setDaemon(true);
backgroundThread.start();
}
private void removeEmptyQuizzes(List<Quiz> quizList){
for(Quiz q : quizList){
if(q.getTaskList().isEmpty()){
quizList.remove(q);
}
}
Platform.runLater(() -> setItems(FXCollections.observableArrayList(quizList)));
}
}
Here's the listener of the ComboBox
with Teachers
which updates the items
private void teacherComboBoxListener(){
view.getTeacherComboBox().getSelectionModel().selectedItemProperty().addListener(new ChangeListener<Teacher>() {
@Override
public void changed(ObservableValue<? extends Teacher> arg0, Teacher oldValue, Teacher newValue) {
view.getQuizComboBox().setValue(null);
if(newValue == view.getAllTeachersChoice()){
List<Quiz> quizList = new QuizDao().getAllQuizes();
view.getQuizComboBox().startTask(quizList);
}
else {
Teacher selectedTeacher = (Teacher) view.getTeacherComboBox().getSelectionModel().getSelectedItem();
List<Quiz> quizList = selectedTeacher.getQuizList();
view.getQuizComboBox().startTask(quizList);
}
}
});
}
I'm getting the following exception
Exception in thread "Thread-5" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at view.controlElements.QuizComboBox.removeEmptyQuizzes(QuizComboBox.java:51)
at view.controlElements.QuizComboBox.lambda$startTask$1(QuizComboBox.java:44)
at java.lang.Thread.run(Thread.java:745)
EDIT: I've tried @fabian's solution and it works with quite a big bug, it removes all the Quizzez
with empty TaskList
from my database.
private void removeEmptyQuizzes(List<Quiz> quizList){
for(Iterator<Quiz> iterator = quizList.iterator(); iterator.hasNext();){
Quiz q = iterator.next();
if(q.getTaskList().isEmpty()){
iterator.remove(); // removed from db also
}
}
Platform.runLater(() -> setItems(FXCollections.observableArrayList(quizList)));
}