2017-01-11 1 views
-1

테이블보기에 사용자 정의 편집 가능한 표 셀이 있습니다. 이제, 내가 가진 문제는 테이블이 생성 될 때 commitEdit() 함수가 실행된다는 것입니다. 문제는 데이터베이스의 항목을 업데이트 할 때 프로그램이 느려지고 모든 항목이 업데이트된다는 것입니다.테이블 만들기시 CommitEdit 함수가 실행됩니다.

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); 
     } 
    }); 
+0

] 선택 값의 어떤 변화에 데이터베이스와 통신을하게됩니다,이 경우에도 'updateItem' 메쏘드에 의해 야기 된 ... – fabian

+0

흠, 그건 허풍입니다. –

+0

죄송 합니다만, 나는이 질문을 이해하지 못하고 있습니다. 'if' 문에 여분의 조건을 넣으면서 아무것도 변경되지 않으면'processEdit'를 호출하지 않는 것이 어떻습니까? –

답변

0

솔루션을 @James_D합니다 - 감사 James_D

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