[英]How to read specific colum in JTable?
我向表中添加数据后想要一个总金额列,我有一个称为PRODUCT(item,qty,price for 1Kg,amount)的表。 所以我知道如何从表中读取所有数据。 但我不知道如何读取和添加一个字段
这是我的代码:
DBconnector db = new DBconnector();
db.connect();
DefaultTableModel dtm = (DefaultTableModel) tblOrder.getModel();
int numRow = dtm.getRowCount();//get rows
int numCol = dtm.getColumnCount();//get colums
ArrayList<Object> list = new ArrayList<Object>();
for (int i = 0 ; i < numRow ; i++){
for (int j = 0 ; j < numCol ; j++){
list.add(tblOrder.getValueAt(i, j));
}
System.out.println(list);
}
任何人都可以帮助我做到这一点吗?
我将假设您的“金额”列中包含数字,而没有货币(因此是5.12而不是$ 5.12)
int numRow = dtm.getRowCount();
int columnAmount = 3; // TODO Set this to the correct column.
double totalAmount = 0.0;
for (int i = 0; i < numRow; i++) { // Loop over all rows
// Add the value from column 'amount' to the total:
totalAmount += Double.parseDouble(tblOrder.getValueAt(i, columnAmount).toString());
}
// TODO Do something with totalAmount
System.out.println(totalAmount);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.