Wrong index when select rows in JTable

I have a popup menu to delete multiple rows in JTable. This table has a column with boolean value (true/false). if value of this column is true, will delete this row. But indexes array was selected is wrong. Example: select rows with indexes are 2,3,4 but the result is 0,2,3. The first row was always selected. If select multiple rows without condition, result is correct.

Anyone can help me?

this is sample code (using Netbeans):

private void menuDeleteLOANActionPerformed(java.awt.event.ActionEvent evt) {                                               
        int[] rows = this.tabMAIN.getSelectedRows();
        try {           
            for(int i = rows.length-1; i >= 0; i--){
                boolean temp = ((Boolean)this.tabMAIN.getValueAt(i, 8)).booleanValue();
                if(temp == true){
                    System.out.println("ID "+this.tabMAIN.getValueAt(i, 3)+((Boolean)this.tabMAIN.getValueAt(i, 8)).booleanValue());
                }else{
                     System.out.println("ID "+this.tabMAIN.getValueAt(i, 3)+((Boolean)this.tabMAIN.getValueAt(i, 8)).booleanValue());
                }
             }          
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }               

Upvotes: 0

Views: 486

Answers (2)

Deepak Odedara
Deepak Odedara

Reputation: 491

try by converting row index to model index

 for (int z = 0; z < tblQue1.getRowCount();) {

                if ((Boolean) tblQue1.getValueAt(z, 4) == true) {

                    dmQue1.removeRow(tblQue1.convertRowIndexToModel(z));
                } else {
                    z++;
                }
            }

Upvotes: 1

makasprzak
makasprzak

Reputation: 5220

You are not accessing the values of the array. This should me more correct:

private void menuDeleteLOANActionPerformed(java.awt.event.ActionEvent evt) {                                               
        int[] rows = this.tabMAIN.getSelectedRows();
        try {           
            for(int i = rows.length-1; i >= 0; i--){
                boolean temp = ((Boolean)this.tabMAIN.getValueAt(rows[i], 8)).booleanValue();
                if(temp == true){
                    System.out.println("ID "+this.tabMAIN.getValueAt(rows[i], 3)+((Boolean)this.tabMAIN.getValueAt(rows[i], 8)).booleanValue());
                }else{
                     System.out.println("ID "+this.tabMAIN.getValueAt(rows[i], 3)+((Boolean)this.tabMAIN.getValueAt(rows[i], 8)).booleanValue());
                }
             }          
        } catch (Exception ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }   

Upvotes: 2

Related Questions