简体   繁体   English

如何在JTable中读取特定的列?

[英]How to read specific colum in JTable?

I have a Table called PRODUCT(item,qty,price for 1Kg,amount) after i added data into table i want to get total amount of amount column. 我向表中添加数据后想要一个总金额列,我有一个称为PRODUCT(item,qty,price for 1Kg,amount)的表。 So I know how to read all data from table. 所以我知道如何从表中读取所有数据。 But i don't know how to read and add one field 但我不知道如何读取和添加一个字段

here's my code: 这是我的代码:

    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);
        }

Anyone please can help me to do this? 任何人都可以帮助我做到这一点吗?

I will assume your column 'amount' contains numbers, without a currency (so 5.12 instead of $5.12) 我将假设您的“金额”列中包含数字,而没有货币(因此是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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM