编辑另一个单元格时设置单元格值

Set cell value when editing another cell

我已阅读 this 但我看不太清楚答案。

我想做的是从 TableColumn 中获取单元格内的值,当我将一个值插入另一个单元格时计算...

一小段代码。

colExistenciaF.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
    colExistenciaF.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<Productos, Double>>() {
        @Override
        public void handle(CellEditEvent<Productos, Double> event) {
            event.getTableView().getItems().get(event.getTablePosition().getRow())
                    .setExistencia(event.getNewValue());

            // Code to place the calculated value in that other column
        }
    });

一个观察,我的"Existencia Actual"列是这样声明的:

@FXML
private TableColumn<Productos, Double> colExistenciaI;

以及 "Existencia Actual" 和 "Diferencia" 列这一列:

@FXML
private TableColumn<Productos, Double> colExistenciaF;
@FXML
private TableColumn<Movproductos, Double> colDiferencia;

这是因为我有实体存储初始库存 (Producto.class) 或产品的存在,但当我需要进行交易或移动时,我使用另一个实体 (MovProducto.class).我不知道这是否相关,但不得不提及以防万一您需要更多背景信息。

编辑


为了给你更多的上下文,我正在使用休眠,所以我的 pojos classes 不能使用(据我所知)StringProperty 或数据库中不存在的任何类型的数据类型.

这些是我在本期中使用的 pojos:

这些是我的控制器(忽略 class DAO 的坏名字)

终于database and the FXML file


提前感谢 James_D 博士。 :)

我创建了一个您可以查看的简单示例。

public class Controller implements Initializable {

    @FXML
    private TableView<Model> table;

    @FXML
    private TableColumn<Model, Double> initial;
    @FXML
    private TableColumn<Model, Double> actual;
    @FXML
    private TableColumn<Model, Double> diff;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initial.setCellValueFactory(data -> data.getValue().initialProperty().asObject());
        actual.setCellValueFactory(data -> data.getValue().actualProperty().asObject());
        diff.setCellValueFactory(data -> data.getValue().differenceProperty().asObject());

        initial.setCellFactory(factory -> new TextFieldTableCell<>(new DoubleConverter()));
        actual.setCellFactory(factory -> new TextFieldTableCell<>(new DoubleConverter()));

        table.setEditable(true);
        diff.setEditable(false); // I think it makes sense to set non-editable

        ObservableList<Model> data = FXCollections.observableArrayList();

        data.add(new Model(10D, 9D));
        data.add(new Model(98D, 45D));

        table.setItems(data);
    }

    class Model {

        private DoubleProperty initial;
        private DoubleProperty actual;
        private DoubleProperty difference;

        public Model(Double initial, Double actual) {
            this.initial = new SimpleDoubleProperty(initial);
            this.actual = new SimpleDoubleProperty(actual);
            this.difference = new SimpleDoubleProperty(initial - actual);
            // Here is where the "magic" happens :)
            this.difference.bind(this.initial.subtract(this.actual));
        }

        public double getInitial() {
            return initial.get();
        }

        public DoubleProperty initialProperty() {
            return initial;
        }

        public double getActual() {
            return actual.get();
        }

        public DoubleProperty actualProperty() {
            return actual;
        }

        public double getDifference() {
            return difference.get();
        }

        public DoubleProperty differenceProperty() {
            return difference;
        }
    }

    class DoubleConverter extends StringConverter<Double> {

        @Override
        public String toString(Double object) {
            return object.toString();
        }

        @Override
        public Double fromString(String string) {
            return Double.valueOf(string);
        }
    }

}

当然你可以在那里处理一些事情,比如NumberformatException。但是你想解决我认为它有效。

已解决

步骤:

  1. 使用属性访问而不是字段访问。以 post for more info, you can also use this gists 为例。
  2. 完成第 1 步后,您可以在需要计算的列中使用此代码(根据您的需要调整代码)。我从这个 .

    得到了灵感
        colDiferencia.setCellValueFactory((cellData) -> {
        cellData.getValue().DiferenciaExistenciaProperty()
                .bind(cellData.getValue().ProductosExistenciaActual().subtract(
                        Double.valueOf(String.valueOf(cellData.getValue().ProductosExistenciaInicialProperty()))));
        return cellData.getValue().DiferenciaExistenciaProperty().asObject();
    });
    
    colExistenciaF.setCellFactory((d) -> {
        TableCell<Productos, Double> cell = new TableCell<Productos, Double>() {
            @Override
            protected void updateItem(Double item, boolean empty) {
                super.updateItem(item, empty);
                setText(String.valueOf(item));
            }
    
        };
        return cell;
    });
    

用这个方法你会解决问题,但我很确定你和我一样,会遇到一些其他的错误或问题。

现在我可以结束这个问题了,bot,然后再感谢所有参与的人。

非常感谢<3 . @James_D 和@Sunflame 我希望你能看到我的练习项目并帮助我提高更多嘿嘿!再次感谢。