簡體   English   中英

在JTable中選擇行時索引錯誤

[英]Wrong index when select rows in JTable

我有一個彈出菜單刪除JTable多行。 該表的列為布爾值(真/假)。 如果此列的值為true,則將刪除此行。 但是索引數組被選擇是錯誤的。 示例:選擇具有索引的行是2,3,4,但結果是0,2,3。 第一行始終處於選中狀態。 如果選擇多個無條件的行,則結果正確。

有人可以幫助我嗎?

這是示例代碼(使用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);
        }
    }               

您沒有訪問數組的值。 我應該更正確:

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

嘗試通過將行索引轉換為模型索引

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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