2012-05-25 4 views
2

방금 ​​TablView를 만들고 데이터를 입력하는 데 성공했습니다. 문제는 업데이트 할 셀을 두 번 클릭하면 업데이트 된 데이터가 표시되지만 클릭하면 업데이트 된 셀에 이전 데이터가 표시된다는 것입니다. 여기javafx 2.1 TableView 업데이트

코드 :

table = new TableView<InvoiceLine>(); 
         table.setEditable(true); 


         Callback<TableColumn, TableCell> cellFactory = 
           new Callback<TableColumn, TableCell>() { 

            public TableCell call(TableColumn p) { 
             return new EditingCell(); 
            } 
           }; 

         ObservableList<InvoiceLine> data = FXCollections.observableArrayList(invoice_Lines); 
         table.setItems(data); 


         designationCol = new TableColumn("Designation"); 
         designationCol.onEditCommitProperty(); 
         designationCol.setCellValueFactory(new PropertyValueFactory<InvoiceLine, String>("invoicelineDesignation")); 
         designationCol.setCellFactory(cellFactory); 
         designationCol.setOnEditCommit(new EventHandler<CellEditEvent<InvoiceLine, String>>() { 

          @Override 
          public void handle(CellEditEvent<InvoiceLine, String> t) { 
           ((InvoiceLine) t.getTableView().getItems().get(
             t.getTablePosition().getRow())).setInvoicelineDesignation(t.getNewValue()); 
           System.out.println(t.getNewValue()); 
          } 
         }); 

         TableColumn quantiteCol = new TableColumn("Quantite"); 

         quantiteCol.setCellValueFactory(
           new PropertyValueFactory<InvoiceLine, String>("invoicelineQuantity")); 
         quantiteCol.setCellFactory(cellFactory); 

         quantiteCol.setOnEditCommit(
           new EventHandler<CellEditEvent<InvoiceLine, String>>() { 

            @Override 
            public void handle(CellEditEvent<InvoiceLine, String> t) { 
             ((InvoiceLine) t.getTableView().getItems().get(
               t.getTablePosition().getRow())).setInvoicelineQuantity(t.getNewValue()); 
             System.out.println(t.getNewValue()); 
            } 
           }); 

         TableColumn puCol = new TableColumn("Prix Unitaire"); 

         puCol.setCellValueFactory(
           new PropertyValueFactory<InvoiceLine, String>("invoicelineUnitPrice")); 
         puCol.setCellFactory(cellFactory); 

         puCol.setOnEditCommit(
           new EventHandler<CellEditEvent<InvoiceLine, String>>() { 

            @Override 
            public void handle(CellEditEvent<InvoiceLine, String> t) { 
             ((InvoiceLine) t.getTableView().getItems().get(
               t.getTablePosition().getRow())).setInvoicelineUnitPrice(t.getNewValue()); 
             System.out.println(t.getNewValue()); 
            } 
           }); 

         TableColumn totalCol = new TableColumn("Total"); 

         totalCol.setCellValueFactory(
           new PropertyValueFactory<InvoiceLine, String>("invoicelineAmount")); 
         totalCol.setCellFactory(cellFactory); 


         totalCol.setOnEditCommit(
           new EventHandler<CellEditEvent<InvoiceLine, String>>() { 

            @Override 
            public void handle(CellEditEvent<InvoiceLine, String> t) { 
             ((InvoiceLine) t.getTableView().getItems().get(
               t.getTablePosition().getRow())).setInvoicelineAmount(t.getNewValue()); 
             System.out.println(t.getNewValue()); 
            } 
           }); 

         table.getColumns().addAll(designationCol, quantiteCol, puCol, totalCol); 
         centerSplitPane.setBottom(table); 
        } 
       }); 

누군가가 나를 도울 수하세요? thks

답변

1

TableView 공식 튜토리얼을 따른 경우 셀의 편집 모드에서 ENTER 키를 눌러 변경 사항을 적용하십시오. 또는 추가하여 EditingCell 소스 코드를 편집 :

textField.focusedProperty().addListener(new ChangeListener<Boolean>() { 

    @Override 
    public void changed(ObservableValue<? extends Boolean> arg0, Boolean arg1, Boolean arg2) { 
     if (!arg2) { 
      commitEdit(textField.getText()); 
     } 
    } 
}); 

createTextField() 방법으로. textField이 포커스를 잃으면 변경 사항이 적용됩니다.

+0

좋은 해결책 Uluk. JavaFX 문서 작업 팀에게 초점 변경 커밋 논리를 공식 자습서에 포함 할 것을 요청하는 이메일을 보냈습니다. – jewelsea

+0

고마워요. 잘 작동합니다. 방금 내가 준 방법을 아래에 추가했습니다. U는 다시 한 번 멋지다. –

관련 문제