【发布时间】:2015-11-10 02:17:51
【问题描述】:
鉴于我有一个tableView,我如何跟踪所有更改,即添加/删除新行,其中一个表格单元格的值已更改,然后关闭当检测到任何这些更改时发生相同的事件?
目前,我有以下代码,如果我连续添加,它只会设法检测到更改。当我编辑任何行的表格单元格和删除一行时,它无法检测到更改。
ObservableList<Trade> observableListOfTrades =FXCollections.observableArrayList();
observableListOfTrades.add(newTrade);
fxTransactionLog.getItems().add(observableListOfTrades.get(observableListOfTrades.size()-1));
observableListOfTrades.addListener(new ListChangeListener() {
@Override
public void onChanged(ListChangeListener.Change change) {
System.out.println("Detected a change! ");
}
});
根据要求,我发布了我的Trade 班级
public class Trade{
// properties
private ObjectProperty<LocalDate> transactionDate;
private StringProperty itemName;
private StringProperty buySell;
private DoubleProperty volume;
private DoubleProperty price;
private DoubleBinding transactionFee;
public Trade(BuySell buySell, LocalDate transactionDate, double volume, double price){
this.buySell = new SimpleStringProperty(buySell.toString());
this.itemName = new SimpleStringProperty("testing");
this.transactionDate = new SimpleObjectProperty<LocalDate>(transactionDate);
this.volume = new SimpleDoubleProperty(volume);
this.price = new SimpleDoubleProperty(price);
}
// getters
public String getBuySell(){
return this.buySell.get();
}
// return Property Object
public StringProperty buySellProperty(){
return this.buySell;
}
// setters
public void setBuySell(String buySell){
this.buySell.set(buySell);
}
public LocalDate getTransactionDate(){
return this.transactionDate.getValue();
}
public ObjectProperty<LocalDate> transactionDateProperty(){
return this.transactionDate;
}
public void setTransactionDate(LocalDate transactionDate){
this.transactionDate.set(transactionDate);
}
public double getVolume(){
return this.volume.get();
}
public DoubleProperty volumeProperty(){
return this.volume;
}
public void setVolume(double volume){
this.volume.set(volume);
}
public double getPrice(){
return this.price.get();
}
public DoubleProperty priceProperty(){
return this.price;
}
public void setPrice(double price){
this.price.set(price);
}
public String getItemName(){
return this.itemName.getValue();
}
public double getTransactionFee(){
this.transactionFee = this.price.multiply(this.volume).multiply(0.15);
return this.transactionFee.getValue();
}
public DoubleBinding transactionFeeProperty(){
return this.transactionFee;
}
public String toString(){
return "Buy: " + getBuySell() + ", Transasction date: " + getTransactionDate() + ", Volume: " + getVolume() + ", Price: " + getPrice() + ", Transaction fee: " + getTransactionFee() ;
}
}
【问题讨论】:
-
您的
Trade类是否使用JavaFX 属性? (您可以发布该课程的代码吗?) -
是的。 Trade 类包含使用 JavaFX 属性的变量。例如,
private ObjectProperty<LocalDate> transactionDate; private StringProperty itemName; private StringProperty buySell; private DoubleProperty volume; private DoubleProperty price; -
简短的回答是使用提取器创建您的“ObservableList”。当我回到电脑上时会发布一个真正的答案。
-
非常感谢!期待看到你的答案!