javafx 2.1更新TableView [英] javafx 2.1 Updating TableView

查看:150
本文介绍了javafx 2.1更新TableView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I just created a TablView and i succeeded to put data into it. The problem is that when I double click on a cell (to update) it shows the updated data but when I click away the updated cell display the old data !

这里代码:

    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

Please can someone help me ? thks

推荐答案

如果你已经按照TableView的官方教程,那么在编辑模式中点击ENTER键要提交更改的单元格。或者通过添加以下内容编辑您的 EditingCell 源代码:

If you have followed the official tutorial of TableView then hit the ENTER key in the editing mode of the cell to commit changes. Alternatively edit your EditingCell source code by adding:

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

into the createTextField() method. This will commit changes when the textField looses its focus.

这篇关于javafx 2.1更新TableView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆