-1

I have a custom editable table cell for my table view. Now, the issue I have is that the commitEdit() function gets executed when the table is created. The issue is that it slows down the program as I'm updating items in my database and every single item gets updated.

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: Solution - thanks to @James_D

classroomChoiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
        if (newValue != null && newValue != getItem()) {
            processEdit(newValue);
        }
    });
  • ANY change of the choicebox value will cause a communication with the database, even if it's caused by the `updateItem` method... – fabian Jan 11 '17 at 21:04
  • Hm, that's a bummer. – Honza Štefánik Jan 11 '17 at 21:20
  • I'm sorry, I am just not understanding this question. Why don't you just put an extra condition in the `if` statement so you don't call `processEdit` if nothing has changed? – James_D Jan 11 '17 at 21:28
  • @James_D I tried to add `if (newValue != null && newValue != oldValue)` to the `if` statement, but it still get's executed. I also tried setting a `boolean` var in the `ChoiceBoxCell` and only allow to update items when it's `true` (which I'd set after my `TableView` was created), but it didn't help. – Honza Štefánik Jan 12 '17 at 17:10
  • `newItem` will never be equal to `oldItem`, else it wouldn't trigger a change. You need to compare it to the cell's item. – James_D Jan 12 '17 at 17:11
  • Ohh, now it works. I have to say, big thank you once again. – Honza Štefánik Jan 12 '17 at 17:20

1 Answers1

0

Solution - thanks to James_D

classroomChoiceBox.getSelectionModel().selectedItemProperty().addListener((obs, oldValue, newValue) -> {
        if (newValue != null && newValue != getItem()) {
            processEdit(newValue);
        }
    });