我的表视图有一个自定义的可编辑表格单元格。现在,我遇到的问题是在创建表时执行public class ChoiceBoxCell extends TableCell<Student, Classroom> {
ChoiceBox<Classroom> classroomChoiceBox;
public ChoiceBoxCell(ObservableList<Classroom> classroomObservableList) {
classroomChoiceBox = new ChoiceBox<>();
classroomChoiceBox.setItems(classroomObservableList);
classroomChoiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null) {
processEdit(newValue);
}
});
}
private void processEdit(Classroom value) {
commitEdit(value);
classroomChoiceBox.setValue(value);
setGraphic(classroomChoiceBox);
}
@Override
public void cancelEdit() {
super.cancelEdit();
setGraphic(classroomChoiceBox);
}
@Override
public void commitEdit(Classroom value) { // gets executed on start up
super.commitEdit(value);
Student student = (Student) getTableRow().getItem();
student.setClassroom(value);
new StudentDao().updateStudent(student); // students get updated for no reason
classroomChoiceBox.setValue(value);
setGraphic(classroomChoiceBox);
}
@Override
public void startEdit() {
super.startEdit();
Classroom value = getItem();
if (value != null) {
classroomChoiceBox.setValue(value);
setGraphic(classroomChoiceBox);
}
}
@Override
protected void updateItem(Classroom item, boolean empty) {
super.updateItem(item, empty);
if (item == null || empty) {
setGraphic(null);
} else {
classroomChoiceBox.setValue(item);
setGraphic(classroomChoiceBox);
}
}
}
函数。问题是,当我更新数据库中的项目并且每个项目都得到更新时,它会减慢程序的速度。
classroomChoiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null && newValue != getItem()) {
processEdit(newValue);
}
});
编辑:解决方案 - 感谢@James_D
scala
答案 0 :(得分:0)
解决方案 - 感谢James_D
classroomChoiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null && newValue != getItem()) {
processEdit(newValue);
}
});