我的使用TableView的經驗僅爲3天(搜索和學習).JavaFx tableView對我來說似乎非常複雜。JavaFx TableView在編輯另一個單元格時設置單元格值
我有一個簡單的應用程序與幾列tableview(Name
,Quantity
,MRP
,Total
)。該表是可編輯的。如果編輯了Quantity
單元格,那麼它將執行quantitycolumn*mrpcolumn
的乘法並設置totalcolumn的值。
這裏是被稱爲資料表數據處理類:
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);
}
}
我嘗試添加監聽器在初始化()如下:
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細胞?
你的意思是實時計算在打字時或之後按Enter? – Ihsan
按Enter鍵也是一個解決方案,但我想在輸入時得到它。 – kodr
你應該看看事件監聽器。不幸的是,我不必花時間編寫代碼,但是您應該可以通過使用谷歌並查看下面的代碼:http://stackoverflow.com/questions/28478525/tablevew-edit-cell-when-keyevent-is -thrown – Ihsan