2

我刚刚创建了一个 TablView 并成功地将数据放入其中。问题是当我双击一个单元格(更新)时,它会显示更新的数据,但是当我点击更新的单元格时会显示旧数据!

这里的代码:

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

请问有人可以帮我吗?谢谢

4

1 回答 1

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当失去焦点时,这将提交更改。

于 2012-05-25T16:54:28.030 回答