首页 > 解决方案 > 使用不同列中的 TextField 编辑 JavaFx TableView 单元格

问题描述

我有包含 3 列的 TableView,其中两个是用 TextFields 编辑的,以允许用户在其中输入数据,但是当我从表中检索项目时,它会返回未进入 TextFields 的内容,如图像中不返回单元格中的正确数字

包含样本数据的表格

这是我的代码:

编辑我发现我的 ObservableList(数据)中的问题我向 TextField 添加了操作以使用新值更新数据,但是如何使代码更新不同的行,因为它使用 TextField 中的最后一个值更新值并摆脱按下ENTER 获取 TxtField 中的整个值,因为它忽略最后一个字符,例如,如果在 TxtField 中输入“30”,我得到“3”而不是“30”

    productQuantity.setCellFactory(new Callback <TableColumn, TableCell> ()
    {
        @Override
        public TableCell call(TableColumn param)
        {
            return new TableCell()
            {
                private TextField txtProductQuantity;

                @Override
                protected void updateItem(Object item, boolean empty)
                {
                    super.updateItem(item, empty);

                    if(!empty)
                    {
                        setGraphic(getTextField());
                    }

                    else
                    {
                        setGraphic(null);
                    }
                }

                public TextField getTextField()
                {
                    if(txtProductQuantity == null)
                    {
                        txtProductQuantity = new TextField();

                        txtProductQuantity.setAlignment(Pos.BASELINE_RIGHT);

                        txtPurchaseItemPrice.setOnKeyPressed(e ->
                        {
                            data.get(0).setPurchaseItemPrice(txtPurchaseItemPrice.getText());
                        });
                    }

                    return txtProductQuantity;
                }
            };
        }
    });
    productQuantity.setOnEditCommit(new EventHandler<CellEditEvent<ProductOpeningBalance, String>> ()
    {
        @Override
        public void handle(CellEditEvent<ProductOpeningBalance, String> t)
        {
            ((ProductOpeningBalance)        
                t.getTableView().getItems().get(t.getTablePosition().getRow())
            ).setProductQuantity(t.getNewValue());
        }
    });

从表中检索数据的代码

    size = tableProductOpeningBalance.getItems().size();

    for(int i = 0; i < size; i++)
    {
    System.out.println(tableProductOpeningBalance.getItems().get(i).getPurchaseItemPrice());
    }

请帮忙?

标签: javafxtableviewtextfieldtablecelltablecolumn

解决方案


推荐阅读