-1
Ich habe eine benutzerdefinierte bearbeitbare Tabellenzelle für meine Tabellenansicht. Nun, das Problem, das ich habe, ist, dass die commitEdit()
Funktion ausgeführt wird, wenn die Tabelle erstellt wird. Das Problem ist, dass es das Programm verlangsamt, während ich Elemente in meiner Datenbank aktualisiere und jedes einzelne Element aktualisiert wird.CommitEdit-Funktion wird beim Erstellen der Tabelle ausgeführt
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);
}
}
}
EDIT: Lösung - dank
classroomChoiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
if (newValue != null && newValue != getItem()) {
processEdit(newValue);
}
});
Jede Änderung des Auswahlbox Wert wird eine Kommunikation mit der Datenbank führen, auch wenn es verursacht durch die 'updateItem' Methode ... – fabian
Hm, das ist ein Mist. –
Es tut mir leid, ich verstehe diese Frage einfach nicht. Warum schreiben Sie nicht einfach eine zusätzliche Bedingung in die 'if'-Anweisung, damit Sie' processEdit' nicht aufrufen, wenn sich nichts geändert hat? –