我想删除表所有行,执行有错误;int rows = Authortable.getRowCount();
   
for(int j=0;j<rows;j++)
{
 DefaultTableModel tableModel = (DefaultTableModel) Authortable.getModel();
 
tableModel.removeRow(j);// rowIndex是要删除的行序号 }
报错如下:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 5 >= 5
at java.util.Vector.removeElementAt(Unknown Source)
at javax.swing.table.DefaultTableModel.removeRow(Unknown Source)
at com.gui.MainView.initTable(MainView.java:166)
at com.gui.MainView$2.actionPerformed(MainView.java:74)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(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)

解决方案 »

  1.   

    楼主这样试试:
    DefaultTableModel tableModel = (DefaultTableModel) Authortable.getModel();       
    for(int j=0;j<tableModel.getRowCount();j++)
    {
       tableModel.removeRow(j);// rowIndex是要删除的行序号}
      

  2.   

    肯定不能这样用的,你已经removeRow了,tableModel的实际行数就少了,这肯定就报数组越界错误!LZ直接清空tableModel算了,应该有这个方法,具体忘记了,不好意思
      

  3.   

     ((DefaultTableModel)table.getModel()).getDataVector().removeAllElements();