2

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 !

Here the code:

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

Please can someone help me ? thks

1 Answers1

1

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

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

Uluk Biy
  • 48,655
  • 13
  • 146
  • 153
  • Nice solution Uluk. I sent an email to the JavaFX documentation team asking them to include the focus change commit logic in the official tutorial. – jewelsea May 25 '12 at 18:23
  • Thanks a lot it works fine i just added the method that u gave me below. U are awesome thxs again. – Anis Ben Jemia May 31 '12 at 14:37