簡體   English   中英

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

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM