我使用TableView的经验只有3天(搜索和学习).JavaFx tableView对我来说似乎很复杂。
我有一个简单的应用程序,其中包含几列(Name
,Quantity
,MRP
,Total
)的tableview。该表是可编辑的。如果编辑了Quantity
单元格,那么它将乘以quantitycolumn*mrpcolumn
并设置totalcolumn的值。
这是名为TableData的数据处理类:
public class TableData {
private final SimpleStringProperty name;
private final SimpleStringProperty batch;
private final SimpleStringProperty exp;
private final SimpleIntegerProperty qty;
private final SimpleDoubleProperty mrp;
private final SimpleDoubleProperty amt;
public TableData(String name,String batch,
String exp,int qty,Double mrp,Double amt) {
this.name = new SimpleStringProperty(name);
this.batch = new SimpleStringProperty(batch);
this.exp = new SimpleStringProperty(exp);
this.qty = new SimpleIntegerProperty(qty);
this.mrp = new SimpleDoubleProperty(mrp);
this.amt = new SimpleDoubleProperty(amt);
}
public String getName() {
return name.get();
}
public SimpleStringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public String getBatch() {
return batch.get();
}
public SimpleStringProperty batchProperty() {
return batch;
}
public void setBatch(String batch) {
this.batch.set(batch);
}
public String getExp() {
return exp.get();
}
public SimpleStringProperty expProperty() {
return exp;
}
public void setExp(String exp) {
this.exp.set(exp);
}
public int getQty() {
return qty.get();
}
public SimpleIntegerProperty qtyProperty() {
return qty;
}
public void setQty(int qty) {
this.qty.set(qty);
}
public double getMrp() {
return mrp.get();
}
public SimpleDoubleProperty mrpProperty() {
return mrp;
}
public void setMrp(double mrp) {
this.mrp.set(mrp);
}
public double getAmt() {
return amt.get();
}
public SimpleDoubleProperty amtProperty() {
return amt;
}
public void setAmt(double amt) {
this.amt.set(amt);
}
}
我尝试在initialize()中添加如下侦听器:
tableView.getSelectionModel().selectedItemProperty().addListener(new ChangeListener<TableData>() {
@Override
public void changed(ObservableValue<? extends TableData> observable, TableData oldValue, TableData newValue) {
System.out.println("The value:"+newValue.getQty());
getQtyAmt(newValue);
//getQtyAmt(newValue);
}
});
getQtyAmt方法:
public void getQtyAmt(TableData tableData){
Double am = tableData.getMrp() * tableData.getQty();
tableData.setAmt(am);
}
以下是我如何制作TableView
可修改的内容:
public void editTable(){
nameColumn.setCellFactory(TextFieldTableCell.forTableColumn());
qtyColumn.setCellFactory(TextFieldTableCell.forTableColumn(new IntegerStringConverter()));
expColumn.setCellFactory(TextFieldTableCell.forTableColumn());
mrpColumn.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
amtColumn.setCellFactory(TextFieldTableCell.forTableColumn(new DoubleStringConverter()));
final int[] ad = {0};
nameColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<TableData, String> event) {
((TableData)event.getTableView().getItems().get(event.getTablePosition().getRow())).setName(event.getNewValue());
//TablePosition tp = tableView.getFocusModel().getFocusedCell();
}
});
qtyColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, Integer>>() {
@Override
public void handle(TableColumn.CellEditEvent<TableData, Integer> event) {
((TableData)event.getTableView().getItems().get(event.getTablePosition().getRow())).setQty(event.getNewValue());
ad[0] = event.getTableView().getItems().get(event.getTablePosition().getRow()).getQty();
dynCal();
}
});
expColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, String>>() {
@Override
public void handle(TableColumn.CellEditEvent<TableData, String> event) {
((TableData)event.getTableView().getItems().get(event.getTablePosition().getRow())).setExp(event.getNewValue());
}
});
mrpColumn.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<TableData, Double>>() {
@Override
public void handle(TableColumn.CellEditEvent<TableData, Double> event) {
event.getTableView().getItems().get(event.getTablePosition().getRow()).setMrp(event.getNewValue());
}
});
}
如何实时计算两个单元格(quantitycolumn*mrpcolumn
),然后将结果设置为amtcolumn单元格?
答案 0 :(得分:0)
由于Amt
是从其他属性计算的,因此您应该将其设为只读,或者至少不允许通过构造函数设置它。
最简单的方法就是让它成为你想要的实际计算 -
DoubleBinding amt = qty.multiply(mrp);
由于DoubleBinding
实现ObservableValue<Number>
,因此您可以毫无问题地将其用作表格中的不可编辑值。
另见Bindings