【问题标题】:TableView automatic calculation of values after commitTableView 提交后自动计算值
【发布时间】:2014-08-29 22:36:36
【问题描述】:

我有一个问题,直到我发现但没有按预期工作......

下面是我有一个包含以下列的表格视图 AV1 | AV2 | AV3 | AV4 |和 我希望在任何单元格中输入新值后,SUM 列会自动更新为列总和 AV1: AV4 示例:

更改列 AV1: AV4 中的任何值后,列总和自动更新其值

| AV1 | AV2 | AV3 | AV4 | SUM | |----- |----- |----- |----- |----- | | 5 | 5 | 5 | 5 | 20 | | 1 | 2 | 3 | 4 | 10 | | 3 | 7 | 10 | 5 | 25 |

我的代码:

 col_av4.setCellFactory((TableColumn<Notas, BigDecimal> param) -> new EditingCellNotas());
    col_av4.setOnEditCommit((CellEditEvent<Notas, BigDecimal> t) -> {
        t.getRowValue().setMediaFinal(t.getNewValue());
        t.getRowValue().setMediaGeral(media());
    });

 private BigDecimal media() {
    BigDecimal media = new BigDecimal(0);
    BigDecimal av1, av2, av3, av4;
    av1 = tbl_notas.selectionModelProperty().getValue().getSelectedItem().getAv1();
    System.out.println("AV1" + av1);
    av2 = tbl_notas.selectionModelProperty().getValue().getSelectedItem().getAv2();
    System.out.println("AV2" + av2);
    av3 = tbl_notas.selectionModelProperty().getValue().getSelectedItem().getAv3();
    System.out.println("AV3" + av3);
    av4 = tbl_notas.selectionModelProperty().getValue().getSelectedItem().getAv4();
    System.out.println("AV4" + av4);
    try {
        media = (av1.add(av2).add(av3).add(av4)).divide(BigDecimal.valueOf(4));

    } catch (Exception e) {
    }

    return media;
}

此代码有效,但我需要双击列单元格总和 更新新值

更新类

我按照这个例子https://gist.github.com/james-d/e485ac525c71e20bb453 现在如何按照响应中给出的示例绑定值?

这是我的课堂笔记,我把属性改成了double。

package com.group.sisgefx.model;
// Generated 09/07/2014 20:05:25 by Hibernate Tools 3.6.0

 import java.math.MathContext;
 import javafx.beans.property.DoubleProperty;
 import javafx.beans.property.IntegerProperty;
 import javafx.beans.property.SimpleDoubleProperty;
 import javafx.beans.property.SimpleIntegerProperty;
 import javafx.beans.property.SimpleStringProperty;
 import javafx.beans.property.StringProperty;
 import javax.persistence.Access;
 import javax.persistence.AccessType;
 import javax.persistence.Column;
 import javax.persistence.Entity;
 import javax.persistence.FetchType;
 import javax.persistence.GeneratedValue;
 import static javax.persistence.GenerationType.IDENTITY;
 import javax.persistence.Id;
 import javax.persistence.JoinColumn;
 import javax.persistence.ManyToOne;
 import javax.persistence.Table;

 /**
 * Notas generated by hbm2java
 */
 @Entity
 @Access(AccessType.PROPERTY)
 @Table(name = "notas", catalog = "sisge")
 public class Notas implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private static final MathContext mc = new MathContext(4);

private IntegerProperty _codNota;
private DoubleProperty _av1;
private IntegerProperty _faltas1;
private DoubleProperty _av2;
private IntegerProperty _faltas2;
private DoubleProperty _av3;
private IntegerProperty _faltas3;
private DoubleProperty _av4;
private IntegerProperty _faltas4;
private DoubleProperty _exame;
private DoubleProperty _mediaGeral;
private DoubleProperty _mediaFinal;
private StringProperty _resultado;
private IntegerProperty _totalFaltas;

private Integer codNota;
private Disciplinas disciplinas;
private Aluno aluno;
private Turma turma;
private Double av1;
private Integer faltas1;
private Double av2;
private Integer faltas2;
private Double av3;
private Integer faltas3;
private Double av4;
private Integer faltas4;
private Double exame;
private Double mediaGeral;
private Double mediaFinal;
private String resultado;
private Integer totalFaltas;

public Notas() {

}

@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "cod_nota", unique = true, nullable = false)
public Integer getCodNota() {
    if (_codNota == null) {
        return this.codNota;
    } else {
        return _codNota.get();
    }
}

public void setCodNota(Integer codNota) {
    if (_codNota == null) {
        this.codNota = codNota;
    } else {
        this._codNota.set(codNota);
    }
}

//get Property
public IntegerProperty _codNotaProperty() {
    if (_codNota == null) {
        _codNota = new SimpleIntegerProperty(this, "codNota", codNota);
    }
    return _codNota;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idDisciplina", nullable = false)
public Disciplinas getDisciplinas() {
    return this.disciplinas;
}

public void setDisciplinas(Disciplinas disciplinas) {
    this.disciplinas = disciplinas;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "matricula", nullable = false)
public Aluno getAluno() {
    return this.aluno;
}

public void setAluno(Aluno aluno) {
    this.aluno = aluno;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "idTurma", nullable = false)
public Turma getTurma() {
    return this.turma;
}

public void setTurma(Turma turma) {
    this.turma = turma;
}

@Column(name = "av1", precision = 4)
public Double getAv1() {
    if (_av1 == null) {
        return this.av1;
    } else {
        return _av1.get();
    }
}

public void setAv1(Double av1) {
    if (this._av1 == null) {
        this.av1 = av1;
    } else {
        this._av1.set(av1);
    }
}

//get Property
public DoubleProperty _av1Property() {
    if (_av1 == null) {
        _av1 = new SimpleDoubleProperty(this, "av1", av1);
    }
    return _av1;
}

@Column(name = "faltas_1")
public Integer getFaltas1() {
    if (_faltas1 == null) {
        return this.faltas1;
    } else {
        return _faltas1.get();
    }
}

public void setFaltas1(Integer faltas1) {
    if (this._faltas1 == null) {
        this.faltas1 = faltas1;
    } else {
        this._faltas1.set(faltas1);
    }
}

//get Property
public IntegerProperty _faltas1Property() {
    if (_faltas1 == null) {
        _faltas1 = new SimpleIntegerProperty(this, "faltas1", faltas1);
    }
    return _faltas1;
}

@Column(name = "av2", precision = 4)
public Double getAv2() {
    if (_av2 == null) {
        return this.av2;
    } else {
        return _av2.get();
    }
}

public void setAv2(Double av2) {
    if (this._av2 == null) {
        this.av2 = av2;
    } else {
        this._av2.set(av2);
    }
}

//get Property
public DoubleProperty _av2Property() {
    if (_av2 == null) {
        _av2 = new SimpleDoubleProperty(this, "av2", av2);
    }
    return _av2;
}

@Column(name = "faltas_2")
public Integer getFaltas2() {
    if (_faltas2 == null) {
        return this.faltas2;
    } else {
        return _faltas2.get();
    }
}

public void setFaltas2(Integer faltas2) {
    if (this._faltas2 == null) {
        this.faltas2 = faltas2;
    } else {
        this._faltas2.set(faltas2);
    }
}

//get Property
public IntegerProperty _faltas2Property() {
    if (_faltas2 == null) {
        _faltas2 = new SimpleIntegerProperty(this, "faltas2", faltas2);
    }
    return _faltas2;
}

@Column(name = "av3", precision = 4)
public Double getAv3() {
    if (_av3 == null) {
        return this.av3;
    } else {
        return _av3.get();
    }
}

public void setAv3(Double av3) {
    if (this._av3 == null) {
        this.av3 = av3;
    } else {
        this._av3.set(av3);
    }
}

//get Property
public DoubleProperty _av3Property() {
    if (_av3 == null) {
        _av3 = new SimpleDoubleProperty(this, "av3", av3);
    }
    return _av3;
}

@Column(name = "faltas3")
public Integer getFaltas3() {
    if (_faltas3 == null) {
        return this.faltas3;
    } else {
        return _faltas3.get();
    }
}

public void setFaltas3(Integer faltas3) {
    if (this._faltas3 == null) {
        this.faltas3 = faltas3;
    } else {
        this._faltas3.set(faltas3);
    }
}

//get Property
public IntegerProperty _faltas3Property() {
    if (_faltas3 == null) {
        _faltas3 = new SimpleIntegerProperty(this, "faltas3", faltas3);
    }
    return _faltas3;
}

@Column(name = "av4", precision = 4)
public Double getAv4() {
    if (_av4 == null) {
        return this.av4;
    } else {
        return _av4.get();
    }
}

public void setAv4(Double av4) {
    if (this._av4 == null) {
        this.av4 = av4;
    } else {
        this._av4.set(av4);
    }
}

//get Property
public DoubleProperty _av4Property() {
    if (_av4 == null) {
        _av4 = new SimpleDoubleProperty(this, "av4", av4);
    }
    return _av4;
}

@Column(name = "faltas_4")
public Integer getFaltas4() {
    if (_faltas4 == null) {
        return this.faltas3;
    } else {
        return _faltas4.get();
    }
}

public void setFaltas4(Integer faltas4) {
    if (this._faltas4 == null) {
        this.faltas4 = faltas4;
    } else {
        this._faltas4.set(faltas4);
    }
}

//get Property
public IntegerProperty _faltas4Property() {
    if (_faltas4 == null) {
        _faltas4 = new SimpleIntegerProperty(this, "faltas4", faltas4);
    }
    return _faltas4;
}

@Column(name = "exame", precision = 4)
public Double getExame() {
    if (_exame == null) {
        return this.exame;
    } else {
        return _exame.get();
    }
}

public void setExame(Double exame) {
    if (this._exame == null) {
        this.exame = exame;
    } else {
        this._av1.set(exame);
    }
}

//get Property
public DoubleProperty _exameProperty() {
    if (_exame == null) {
        _exame = new SimpleDoubleProperty(this, "exame", exame);
    }
    return _exame;
}

@Column(name = "media_geral", precision = 4)
public Double getMediaGeral() {
    if (_mediaGeral == null) {
        return this.mediaGeral;
    } else {
        return _mediaGeral.get();
    }
}

public void setMediaGeral(Double mediaGeral) {
    if (this._mediaGeral == null) {
        this.mediaGeral = mediaGeral;
    } else {
        this._mediaGeral.set(mediaGeral);
    }
}

//get Property
public DoubleProperty _mediaGeralProperty() {
    if (_mediaGeral == null) {
        _mediaGeral = new SimpleDoubleProperty(this, "mediaGeral", mediaGeral);
    }
    return _mediaGeral;
}

@Column(name = "mediaFinal", precision = 4)
public Double getMediaFinal() {
    if (_mediaFinal == null) {
        return this.mediaFinal;
    } else {
        return _mediaFinal.get();
    }
}

public void setMediaFinal(Double mediaFinal) {
    if (this._mediaFinal == null) {
        this.mediaFinal = mediaFinal;
    } else {
        this._mediaFinal.set(mediaFinal);
    }
}

//get Property
public DoubleProperty _mediaFinalProperty() {

    if (_mediaFinal == null) {
        _mediaFinal = new SimpleDoubleProperty(this, "mediaFinal", mediaFinal);
    }
    return _mediaFinal;
}

@Column(name = "resultado", length = 11)
public String getResultado() {
    if (_resultado == null) {
        return this.resultado;
    } else {
        return _resultado.get();
    }
}

public void setResultado(String resultado) {
    if (this._resultado == null) {
        this.resultado = resultado;
    } else {
        this._resultado.set(resultado);
    }
}

//get Property
public StringProperty _resultadoProperty() {
    if (_resultado == null) {
        _resultado = new SimpleStringProperty(this, "resultado", resultado);
    }
    return _resultado;
}

@Column(name = "total_faltas")
public Integer getTotalFaltas() {
    if (_totalFaltas == null) {
        return this.totalFaltas;
    } else {
        return _totalFaltas.get();
    }
}

public void setTotalFaltas(Integer totalFaltas) {
    if (this._totalFaltas == null) {
        this.totalFaltas = totalFaltas;
    } else {
        this._totalFaltas.set(totalFaltas);
    }
}

//get Property
public IntegerProperty _totalFaltasProperty() {
    if (_totalFaltas == null) {
        _totalFaltas = new SimpleIntegerProperty(this, "resultado", totalFaltas);
    }
    return _totalFaltas;
}

}

【问题讨论】:

  • 你的Notas 类中有mediaGeralProperty() 访问器吗?
  • 我不能举个例子

标签: java javafx tableview calculator


【解决方案1】:

您应该发布您的 Notas 课程。您可以将 media(或 mediaGeral??)的值绑定到类中的总和。这样,所有更改都会立即更新。绑定 BigDecimal 有点不同,因为您必须创建一个 ObjectBinding。这是一个例子

import java.math.BigDecimal;
import java.math.MathContext;
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.beans.property.SimpleObjectProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import javafx.util.StringConverter;

public class BigDecTable extends Application {
    private static final MathContext mc = new MathContext(5);

    @Override
    public void start(Stage primaryStage) {
        ObservableList<Data> data = FXCollections.observableArrayList(
                new Data(new BigDecimal(123.45,mc),new BigDecimal(123.45,mc)),
                new Data(new BigDecimal(123.45,mc),new BigDecimal(123.45,mc))
        );
        TableView tv = new TableView(data);
        tv.setEditable(true);
        tv.getSelectionModel().setCellSelectionEnabled(true);

        StringConverter bdsc = new StringConverter<BigDecimal>() {

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

            @Override
            public BigDecimal fromString(String string) {
                if(string==null || string.isEmpty()) return BigDecimal.ZERO;
                return new BigDecimal(string,mc);
            }
        };

        TableColumn fld1Col = new TableColumn("fld1");
        fld1Col.setCellValueFactory(new PropertyValueFactory("fld1"));
        fld1Col.setCellFactory(TextFieldTableCell.forTableColumn(bdsc));

        TableColumn fld2Col = new TableColumn("fld2");
        fld2Col.setCellValueFactory(new PropertyValueFactory("fld2"));
        fld2Col.setCellFactory(TextFieldTableCell.forTableColumn(bdsc));

        TableColumn fld3Col = new TableColumn("tot");
        fld3Col.setCellValueFactory(new PropertyValueFactory("fld3"));
        fld3Col.setEditable(false);

        tv.getColumns().addAll(fld1Col,fld2Col,fld3Col);

        StackPane root = new StackPane();
        root.getChildren().add(tv);
        Scene scene = new Scene(root);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public class Data{
        SimpleObjectProperty<BigDecimal> fld1;
        SimpleObjectProperty<BigDecimal> fld2;
        SimpleObjectProperty<BigDecimal> fld3;

        public SimpleObjectProperty<BigDecimal> fld1Property() {return fld1;}
        public SimpleObjectProperty<BigDecimal> fld2Property() {return fld2;}
        public SimpleObjectProperty<BigDecimal> fld3Property() {return fld3;}

        public Data(BigDecimal fld1val, BigDecimal fld2val) {
            this.fld1 = new SimpleObjectProperty<>(fld1val);
            this.fld2 = new SimpleObjectProperty<>(fld2val);
            this.fld3 = new SimpleObjectProperty<>(fld2val.add(fld1val));
            fld3.bind(Bindings.createObjectBinding(() -> 
                    fld1.get().add(fld2.get(),mc), fld1,fld2));
        }
    }
}

编辑: 如果您不使用属性,那么您将无法进行自动更新。由于您的 Notas 类似乎是自动生成的,因此我将在 onEditCommit() 中进行更新。您可以通过隐藏和显示 mediaGeral 列(或任何列)来刷新表格;

    table.getColumns().get(0).setVisible(false);
    table.getColumns().get(0).setVisible(true);

它没有更新,因为表中没有侦听器。如果您只是将 SimpleDoubleProperty 添加到 Notas 类,那么它会起作用,但您仍然需要 media() 方法进行计算。

public class Notas implements java.io.Serializable {
    transient private SimpleDoubleProperty average;
    //note that this won't serialize so you'd have to create it in readObject()

    ...
    //in the constructor
    this.mediaGeral = mediaGeral;
    average = new SimpleDoubleProperty(mediaGeral);
    ...   
    //make a accessor for the property
    public SimpleDoubleProperty averageProperty(){
        return average;
    }
}

然后使用 PropertyValueFactory("average") 制作一个 TableColumn

编辑2: 绑定_mediaGeral使用

_mediaGeral.bind(_av1.add(_av2.add(_av3.add(_av4.add))).divide(4));

在 notas 类的构造函数中。你不能有一个 setter,因为你不能设置一个绑定的属性。

【讨论】:

  • 贴出课堂笔记,整体平均计算如下(+ AV1 + AV2 AV3 AV4 +) / 4
  • 谢谢,仍然无法自动更新,我研究了 SimpleDoubleProperty 以在我的应用程序中实现。
  • 我很确定如果您只执行我添加的 setVisible(false) then true 部分,您的代码将可以工作。如果不需要使用自动生成的代码,最好使用属性。
  • 问题是我的pojo和因为数据库已经存在而自动生成的有可能使用带有JPA注解的属性?
  • 我更新了添加到 tableview 中的 Nota 类属性,现在如何将列 (AV1: AV4) 与 sum 列绑定?使用 media() 方法会自动更新,但我不想使用此方法
猜你喜欢
  • 2017-05-07
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 2023-03-27
  • 2021-12-31
相关资源
最近更新 更多