Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

jTable getSelectedRows

843806Nov 14 2007 — edited Nov 14 2007
Hi,

I have to select rows in a jTable in order to manipulate the data. I have the following code:
int [] selected = tblReport.getSelectedRows();
       int amount = 0;
        for (int i = 0 ; i < selected.length ; i++){
            amount += (Integer.parseInt("" + tblReport.getValueAt(i, 6)));
        }
    txtAmount.setText("" + amount);  
    txtAmount.setEditable(true);
    txtAmount.selectAll();
For some reason it always start counting for row 0, and add the amount of the following rows - according to the number of rows selected, instead of starting from the 1st selected row.

I tested the code with "countSelectedRows()", and as long as the selected rows were consecutive - I got the correct count, however, if I had a non selected row in the middle the count came wrong. I also tested with getSelectedRow() and got the correct index of the 1st selected row, but the amount was always from row 0 of the table down...

Am I using the wrong method?

Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 12 2007
Added on Nov 14 2007
2 comments
427 views