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);
}
});
請某人能幫助我嗎? thk
尼斯解決方案Uluk。我向JavaFX文檔團隊發送了一封電子郵件,要求他們在官方教程中包含焦點更改提交邏輯。 – jewelsea
非常感謝,它可以正常工作,我只是添加了下面給你的方法。你真棒再次。 –