我要说的功能,相信 很多 人都 做过 类似的实现——就是根据一组内容,从JTable中删除内容所在的行;我所试验过的办法有以下几种:1.从 TableModel 的 最后一行 往前查找,一行一行读取 关键列的 内容,和 要删除的内容比较,如果 相同,则删除 此索引号所在的行 private void removeByFileName(String qualifyedFileName) { int count = model.getRowCount();
String fileName;
for (int i = count - 1; i >= 0; i--) {
fileName = (String) model.getValueAt(i, 0);
if (fileName.equals(qualifyedFileName)) {
    model.removeRow(i);
    // System.out.println("Table count:"+count);
    // System.out.println("Table row:"+i);
    // System.out.println("fileName:"+fileName);
    break;
}
}
}
2.先把要删除的内容在TableModel中找到对应的索引号放在一个AarrayList里,保证在 TableModel 的数据没有动过 的情况下,也从最后一行往前搜,每到一行,就在AarrayList找有没有包含这一样,包含则删除;    private void removeByFileNames(ArrayList<String> fileNames) {
     if(fileNames == null || fileNames.size() == 0)return;
     int count = model.getRowCount();
     String qualifyedFileName = "";
     String fileName = "";
     ArrayList<Integer> indexList = new ArrayList<Integer>();
     for(int i = 0; i < fileNames.size(); i++){
     qualifyedFileName = fileNames.get(i);
     for (int k = count - 1; k >= 0; k--) {
     fileName = (String) model.getValueAt(k, 0);
     if (fileName.equals(qualifyedFileName)) {
     indexList.add(k);
     break;
     }
     }     for (int k = count - 1; k >= 0; k--) {
if (indexList.contains(k)) {
model.removeRow(k);
}
}
}
不管 用 什么办法,都出现 如下 的 错误 :Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
at java.util.Vector.elementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.getValueAt(Unknown Source)
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)按说 ArrayIndexOutOfBoundsException 这个 异常 很好懂,平常因为不小心 经常碰到,数组访问越界;可这里的问题是,我想try catch 都捕捉不到异常,而且 这个 异常没有使程序终止,前台上,我要删的的数据也如愿删掉,就是后台这个 异常 时不时出现,让人 很 不爽,ArrayIndexOutOfBoundsException: 2 >= 2 ,这个 2 >= 2,这个数 每次也不一定是 2,我想跟踪 都跟不到,可能很多人 会奇怪,因为我用debug状态去跟,不会出现这个问题,于是 我试着 在里面加了一个暂停 :    try {
        Thread.sleep(200);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

有了这个 问题不在出现!
所以 小弟 无知,开始 怀疑 DefaultTableModel.removeRow(int) 这个方法会不会 是没有同步 造成的 ,我在 我自己的代码里也试着 同步实现,问题照旧 !
请高手 们 指点下吧 !

解决方案 »

  1.   

    swing好像是不同步的,用下面的方法看看
    SwingUtilities.invokeLater( 
        new Runnable() { 
            public void run() { 
                 model.removeRow(k);
            }
        }
    ); 
      

  2.   


    while(index <= model.getRowCount()){
       fileName = (String) model.getValueAt(index, 0);
       if (fileName.equals(qualifyedFileName)) {
           model.removeRow(index);
       }else{
         index++; 
       }
    }
      

  3.   

    while(index   <=   model.getRowCount()){ 
          fileName   =   (String)   model.getValueAt(index,   0); 
          if   (fileName.equals(qualifyedFileName))   { 
                  model.removeRow(index); 
          }else{ 
              index++;   
          } 
    }
      

  4.   

    SwingUtilities.invokeLater( 
        new Runnable() { 
            public void run() { 
                 model.removeRow(k);
            }
        }
    ); 
      

  5.   

    private void removeByFileName(String qualifyedFileName) {    // int count = model.getRowCount();
        String fileName;
        for (int i = model.getRowCount()- 1; i >= 0; i--) {   // <-- count changed here!
            fileName = (String) model.getValueAt(i, 0);
            if (fileName.equals(qualifyedFileName)) {
                model.removeRow(i);
                // System.out.println("Table count:"+count);
                // System.out.println("Table row:"+i);
                // System.out.println("fileName:"+fileName);
                break;
            }
        }
    }
      

  6.   

    这个问题就像的在HTML中删除select 中的一个option一样.
    本来是1-10个下标
    用for循环删除一个后,下标只能是1-9了,但是循环变量却一直是加1,所以是不行的.应该是会自动压缩了.本来是有10,用9这个下标访问是没有问题的,但删除了一个之后,压缩了,再用9访问就会出问题了.
      

  7.   

    swing确实有的时候会抛一些莫名奇妙的异常
      

  8.   

    真搞不懂,这是Java的问题吗?明明了写程序的人的问题嘛!
      

  9.   

    用MVC啊,删除时删M里的,然后重新刷新。
      

  10.   

    说实在话,这个问题很多初学java的朋友肯定也遇到过, 这些都是思维逻辑问题。
      

  11.   

    private   void   removeByFileName(String   qualifyedFileName)   {         //   int   count   =   model.getRowCount(); 
            String   fileName; 
            for   (int   i   =   model.getRowCount()-   1;   i   > =   0;   i--)   {       //   <--   count   changed   here! 
                    fileName   =   (String)   model.getValueAt(i,   0); 
                    if   (fileName.equals(qualifyedFileName))   { 
                            model.removeRow(i); 
                            //   System.out.println("Table   count:"+count); 
                            //   System.out.println("Table   row:"+i); 
                            //   System.out.println("fileName:"+fileName); 
                            break; 
                    } 
            } 
      

  12.   

    JDK DOC
    removeRow
    public void removeRow(int row)
    Removes the row at row from the model. Notification of the row being removed will be sent to all the listeners. 
    Parameters:
    row - the row index of the row to be removed 
    Throws: 
    ArrayIndexOutOfBoundsException - if the row was invalid红色部分说明了removeRow会激发fireTableRowsDeleted或是fireTableDataChanged这样的Event,所以,一旦执行了removeRow,那model的行数会马上改变的。