我遇到问题,直到找到但没有按预期工作......
以下我有一个包含以下列AV1 |的tableview 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 现在如何根据响应中给出的示例绑定值?
这是我的课堂笔记,我改变了双重属性。
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;
}
}
答案 0 :(得分:0)
你应该发布你的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”)
创建一个TableColumnEDIT2: 要绑定_mediaGeral,请使用
_mediaGeral.bind(_av1.add(_av2.add(_av3.add(_av4.add))).divide(4));
在notas类的构造函数中。你不能拥有一个setter,因为你不允许设置绑定属性。