编辑同一行的另一个单元格后,javafx tableview更新单元格

编辑同一行的另一个单元格后,javafx tableview更新单元格

问题描述:

我有两列,一列是 columnCantidad 这是我编辑的那一列,另一列是 columnTotal 应该在上一个操作之后更新。

I have two columns, one is columnCantidad which is the one that I edit and the other is columnTotal which should update after the previous action.

这是我设置列的方式:

  TableColumn<Movimientos, Integer> columnCantidad = new TableColumn<>("Cantidad");
    columnCantidad.setCellValueFactory(new PropertyValueFactory<>("cantidad"));
    columnCantidad.prefWidthProperty().bind(table.widthProperty().multiply(0.20));
    columnCantidad.setResizable(false);
    columnCantidad.setEditable(true);
    columnCantidad.setCellFactory(new Callback<TableColumn<Movimientos, Integer>, TableCell<Movimientos, Integer>>() {

        @Override
        public TableCell<Movimientos, Integer> call(TableColumn<Movimientos, Integer> param) {
            TextFieldTableCell<Movimientos, Integer> myEditableTableCell = new TextFieldTableCell<Movimientos, Integer>(new IntegerStringConverter()) {

                @Override
                public void commitEdit(Integer val) {
                    int index = this.getTableRow().getIndex();
                    Movimientos mov = table.getItems().get(index);
                    mov.setCantidad(val);
                    mov.setTotal(val * mov.getPrecioVenta());
                    super.commitEdit(val);

                    // this is where i should do something because
                    // the value of the cell of columnCantidad is changed
                }
            };

            return myEditableTableCell;
        }
    });

    TableColumn<Movimientos, Double> columnTotal = new TableColumn<>("Total");
    columnTotal.setCellValueFactory(new PropertyValueFactory<>("total"));
    columnTotal.prefWidthProperty().bind(table.widthProperty().multiply(0.30));
    columnTotal.setResizable(false);
    columnTotal.setCellFactory((d) -> {
        TableCell<Movimientos, Double> cell = new TableCell<Movimientos, Double>() {

            @Override
            protected void updateItem(Double item, boolean empty) {
                super.updateItem(item, empty);
                setText(String.valueOf(item));
            }

        };
        return cell;
    });

    table.getColumns().setAll(columnCodigo, columnName, columnCantidad, columnTotal);
    table.getItems().setAll(list);
    table.setEditable(true);

任何想法?我没有尝试任何数据绑定方式,似乎可能是解决方案。

Any ideas? I've not tried any way of databinding, seems like something like that might be the solution.

这是我的实体 Movimientos

@Entity
@Table(name = "movimientos")
@NamedQueries({
    @NamedQuery(name = "Movimientos.findAll", query = "SELECT m FROM `   Movimientos m")})`
public class Movimientos implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "idMovimientos")
    private Integer idMovimientos;
    @Column(name = "Cantidad")
    private Integer cantidad;
    @Basic(optional = false)
    @NotNull
    @Column(name = "fecha_movimiento")
    @Temporal(TemporalType.DATE)
    private Date fechaMovimiento;
    // @Max(value=?)  @Min(value=?)//if you know range of your decimal fields consider using these annotations to enforce field validation
    @Column(name = "precio_venta")
    private Double precioVenta;
    @Column(name = "precio_proveedor")
    private Double precioProveedor;
    @Column(name = "ganancia")
    private Double ganancia;
    @Column(name = "total")
    private Double total;
    @JoinColumn(name = "Inventario_idInventario", referencedColumnName = "idInventario")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Inventario inventarioidInventario;
    @JoinColumn(name = "Tipo_Movimiento_idTipo_Movimiento", referencedColumnName = "idTipo_Movimiento")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private TipoMovimiento tipoMovimientoidTipoMovimiento;
    @JoinColumn(name = "Precio_idPrecio", referencedColumnName = "idPrecio")
    @ManyToOne(fetch = FetchType.LAZY)
    private Precio precioidPrecio;
    @JoinColumn(name = "Usuarios_idUsuarios", referencedColumnName =          "idUsuarios")
    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private Usuarios usuariosidUsuarios;
    @JoinColumn(name = "Venta_idVenta", referencedColumnName = "idVenta")
    @ManyToOne(fetch = FetchType.LAZY)
    private Venta ventaidVenta;

    public Movimientos() {
    }

    public Movimientos(Integer idMovimientos) {
        this.idMovimientos = idMovimientos;
    }

    public Movimientos(Integer idMovimientos, Date fechaMovimiento) {
        this.idMovimientos = idMovimientos;
        this.fechaMovimiento = fechaMovimiento;
    }

    public Integer getIdMovimientos() {
        return idMovimientos;
    }

    public void setIdMovimientos(Integer idMovimientos) {
        this.idMovimientos = idMovimientos;
    }

    public Integer getCantidad() {
        return cantidad;
    }

    public void setCantidad(Integer cantidad) {
        this.cantidad = cantidad;
    }

    public Date getFechaMovimiento() {
        return fechaMovimiento;
    }

    public void setFechaMovimiento(Date fechaMovimiento) {
        this.fechaMovimiento = fechaMovimiento;
    }

    public Double getPrecioVenta() {
        return precioVenta;
    }

    public void setPrecioVenta(Double precioVenta) {
        this.precioVenta = precioVenta;
    }

    public Double getPrecioProveedor() {
        return precioProveedor;
    }

    public void setPrecioProveedor(Double precioProveedor) {
        this.precioProveedor = precioProveedor;
    }

    public Double getGanancia() {
        return ganancia;
    }

    public void setGanancia(Double ganancia) {
        this.ganancia = ganancia;
    }

    public Inventario getInventarioidInventario() {
        return inventarioidInventario;
    }

    public void setInventarioidInventario(Inventario inventarioidInventario) {
        this.inventarioidInventario = inventarioidInventario;
    }

    public TipoMovimiento getTipoMovimientoidTipoMovimiento() {
        return tipoMovimientoidTipoMovimiento;
    }

    public void setTipoMovimientoidTipoMovimiento(TipoMovimiento tipoMovimientoidTipoMovimiento) {
        this.tipoMovimientoidTipoMovimiento = tipoMovimientoidTipoMovimiento;
    }

    public Precio getPrecioidPrecio() {
        return precioidPrecio;
    }

    public void setPrecioidPrecio(Precio precioidPrecio) {
        this.precioidPrecio = precioidPrecio;
    }

    public Usuarios getUsuariosidUsuarios() {
        return usuariosidUsuarios;
    }

    public void setUsuariosidUsuarios(Usuarios usuariosidUsuarios) {
        this.usuariosidUsuarios = usuariosidUsuarios;
    }

    public Venta getVentaidVenta() {
        return ventaidVenta;
    }

    public void setVentaidVenta(Venta ventaidVenta) {
        this.ventaidVenta = ventaidVenta;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idMovimientos != null ? idMovimientos.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Movimientos)) {
            return false;
        }
        Movimientos other = (Movimientos) object;
        if ((this.idMovimientos == null && other.idMovimientos != null) || (this.idMovimientos != null && !this.idMovimientos.equals(other.idMovimientos))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.retail.entity.Movimientos[ idMovimientos=" + idMovimientos + " ]";
    }
}


i必须在我的实体中创建一个javafx属性。所以我可以使用绑定。由于我的原始类是由jpa管理的实体,我必须创建另一个类。

i had to create a javafx property in my Entity. so i could use binding. Since my original class was an entity managed by jpa , i had to create another class.

   TableColumn<MovimientoDTO, Integer> columnCantidad = new TableColumn<>("Cantidad");
    columnCantidad.setCellValueFactory((param) -> param.getValue().cantidadPropertyProperty().asObject());
    columnCantidad.prefWidthProperty().bind(table.widthProperty().multiply(0.20));
    columnCantidad.setResizable(false);
    columnCantidad.setEditable(true);
    columnCantidad.setCellFactory(new Callback<TableColumn<MovimientoDTO, Integer>, TableCell<MovimientoDTO, Integer>>() {

        @Override
        public TableCell<MovimientoDTO, Integer> call(TableColumn<MovimientoDTO, Integer> param) {
            TextFieldTableCell<MovimientoDTO, Integer> myEditableTableCell = new TextFieldTableCell<MovimientoDTO, Integer>(new IntegerStringConverter()) {

                @Override
                public void commitEdit(Integer val) {
                    int index = this.getTableRow().getIndex();
                    MovimientoDTO mov = table.getItems().get(index);
                    mov.setCantidadProperty(val);
                    //mov.setTotal(val * mov.getPrecioVenta());
                    super.commitEdit(val);
                }
            };

            return myEditableTableCell;
        }
    });

    TableColumn<MovimientoDTO, Double> columnTotal = new TableColumn<>("Total");
    columnTotal.setCellValueFactory((param) -> {
        param.getValue().totalPropertyProperty().bind(param.getValue().cantidadPropertyProperty().
                multiply(param.getValue().getPrecioVenta()));
        return param.getValue().totalPropertyProperty().asObject();
    });

    columnTotal.prefWidthProperty().bind(table.widthProperty().multiply(0.30));
    columnTotal.setResizable(false);
    columnTotal.setCellFactory((d) -> {
        TableCell<MovimientoDTO, Double> cell = new TableCell<MovimientoDTO, Double>() {

            @Override
            protected void updateItem(Double item, boolean empty) {
                super.updateItem(item, empty);
                setText(String.valueOf(item));
            }

        };

        return cell;
    });