Swing JTable,从DefaultTableModel中删除行时异常

时间:2015-07-02 17:28:20

标签: java swing jtable indexoutofboundsexception defaulttablemodel

我编写了以下函数,我可以使用它来删除DefaultTableModel中的行:

private static void removeTableRows(JTable tableToClear){
    DefaultTableModel defaultModel = 
            (DefaultTableModel) tableToClear.getModel();
    int rows = defaultModel.getRowCount();

      while(rows>0) {
         defaultModel.removeRow(0);
      }
      String[] test = {"a","b","c"};
     defaultModel.addRow(test);
}

抛出以下异常:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 >= 0
    at java.util.Vector.elementAt(Vector.java:427)
    at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:632)
    at org.swx.nursing.tools.gui.DefaultTableGui$2.tableChanged(DefaultTableGui.java:217)
    at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)
    at javax.swing.table.AbstractTableModel.fireTableRowsDeleted(AbstractTableModel.java:245)
    at javax.swing.table.DefaultTableModel.removeRow(DefaultTableModel.java:447)
    at org.swx.nursing.tools.gui.DefaultTableGui.removeTableRows(DefaultTableGui.java:357)
    at org.swx.nursing.tools.gui.DefaultTableGui.actionPerformed(DefaultTableGui.java:330)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6297)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3275)
    at java.awt.Component.processEvent(Component.java:6062)
    at java.awt.Container.processEvent(Container.java:2039)
    at java.awt.Component.dispatchEventImpl(Component.java:4660)
    at java.awt.Container.dispatchEventImpl(Container.java:2097)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
    at java.awt.Container.dispatchEventImpl(Container.java:2083)
    at java.awt.Window.dispatchEventImpl(Window.java:2489)
    at java.awt.Component.dispatchEvent(Component.java:4488)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:674)
    at java.awt.EventQueue.access$400(EventQueue.java:81)
    at java.awt.EventQueue$2.run(EventQueue.java:633)
    at java.awt.EventQueue$2.run(EventQueue.java:631)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$3.run(EventQueue.java:647)
    at java.awt.EventQueue$3.run(EventQueue.java:645)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:644)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

tablechanged方法被覆盖如下:

 //Add table cell listener
    model.addTableModelListener(new TableModelListener(){

        public void tableChanged(TableModelEvent e) {
             int row = e.getFirstRow();
             int col = e.getColumn();
             TableModel tableModel = (TableModel)e.getSource();
             Object data = null ;

             try{
                 data = tableModel.getValueAt(row, col);
             } catch (Exception ex) {
                 LOGGER.error("Failed to access table data: "+ex.getMessage());
             }

             boolean ishotKeyColEmpty = tableModel.getValueAt(row, 0).toString().isEmpty();
             boolean ishTypeColEmpty = tableModel.getValueAt(row, 2).toString().isEmpty();     
             /*
              * If the Application path column is empty, then clear the contents from
              * the hotKey and Type cells for this row 
              * 
              */
             if(data != null 
                     && data.toString().equals("") 
                     && !ishotKeyColEmpty 
                     && !ishTypeColEmpty) {
                 tableModel.setValueAt("", row, 0);
                 tableModel.setValueAt("", row, 2);
             }      
        }
    });

我不确定导致此异常的原因..

1 个答案:

答案 0 :(得分:3)

您的代码中存在2个问题。

1)尝试从modellistner。{/ p>中的现有行/列中获取值

2)即使没有更多行 - removeTableRows方法

,也不停地删除行

让我先解释第二个错误。

在while循环中

2)你没有更新行计数。你正在获取行计数但是这个while循环条件永远不会变为false。例如,你有3行,现在计数是3而你正在检查循环(rows > 0)无论它循环多长时间总是如此,所以当第四次循环运行时出现错误,因为没有更多行要删除。你必须重新计算行数

将方法更改为

DefaultTableModel defaultModel= (DefaultTableModel) tableToClear.getModel();
int rows = defaultModel.getRowCount();
while (rows > 0) {
       defaultModel.removeRow(0);
       rows = defaultModel.getRowCount(); // this is very important
}
String[] test = {"a", "b", "c"};
defaultModel.addRow(test);


    注意:

您可以使用方法defaultModel.RowCount(0);方法删除所有行。您不需要使用循环来执行此操作

1)确保row和col为0或者超过它。因为当它们不存在时它们可以是-1。然后你会遇到很多错误

所以这一行很重要

 (row >= 0 && col >= 0){}

也在这里

boolean ishotKeyColEmpty = tableModel.getValueAt(row, 0).toString().isEmpty();

在调用之前,您必须安全地检查getValueAt(row, 0)是否为空  toString().isEmpty()方法。  在你的

完整代码

addTableModelListener(new TableModelListener() {

            @Override
            public void tableChanged(TableModelEvent e) {

                int row = e.getFirstRow();
                int col = e.getColumn();
                TableModel tableModel = (TableModel) e.getSource();
                Object data = null;

                try {
                    if (row >= 0 && col >= 0) {
                        data = tableModel.getValueAt(row, col);

                        boolean ishotKeyColEmpty = false;
                        boolean ishTypeColEmpty = false;
                        if (tableModel.getValueAt(row, 0) != null) {
                            ishotKeyColEmpty = tableModel.getValueAt(row, 0).toString().isEmpty();
                        }
                        if (tableModel.getValueAt(row, 2) != null) {
                            ishTypeColEmpty = tableModel.getValueAt(row, 2).toString().isEmpty();
                        }

                        if (data != null && data.toString().equals("") && !ishotKeyColEmpty && !ishTypeColEmpty) {
                            tableModel.setValueAt("", row, 0);
                            tableModel.setValueAt("", row, 2);
                        }
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

            }

        }