jTable填充和刷新ArrayIndexOutOfBoundsException

时间:2015-05-11 09:34:36

标签: java jtable indexoutofboundsexception swingworker

我在SwingWorker下面填充了jTable1。我有执行此任务的按钮。

public class WorkerTime extends SwingWorker<Void, Void> {

   protected Void doInBackground() throws Exception {

        DefaultTableModel model1 = (DefaultTableModel) jTable1.getModel();
        model1.setRowCount(0);

            for (int i = 0; i < 5; i++){

              model1.addRow(new String[]{"a","a","a","a","a","a","a","a","a","a","a"});
              model1.fireTableDataChanged();

            }

    return null;
   }

   public void done() {

   }

}

第一,第二,有时是第三次成功执行。但有时当我点击我的按钮jTable清除但希望5行(a,a,a ...行)没有来,Java抛出错误之下;

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1 >= 0
    at java.util.Vector.elementAt(Vector.java:470)
    at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649)
    at javax.swing.JTable.getValueAt(JTable.java:2717)
    at javax.swing.JTable.prepareRenderer(JTable.java:5719)
    at javax.swing.plaf.synth.SynthTableUI.paintCell(SynthTableUI.java:684)
    at javax.swing.plaf.synth.SynthTableUI.paintCells(SynthTableUI.java:581)
    at javax.swing.plaf.synth.SynthTableUI.paint(SynthTableUI.java:365)
    at javax.swing.plaf.synth.SynthTableUI.update(SynthTableUI.java:276)
    at javax.swing.JComponent.paintComponent(JComponent.java:778)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5219)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1529)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1452)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1249)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5167)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4978)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:808)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:769)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:718)
    at javax.swing.RepaintManager.access$1100(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1677)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

我不明白最近发生了什么。我也无法调试它,因为问题是随机发生的。有时我执行任务并在屏幕上查看数据5-6次,有时1-2次。任何帮助赞赏。 感谢。

--- ---编辑

我的按钮是;

if(radioButtonLF.isSelected() || radioButtonLF2.isSelected() || radioButtonT.isSelected() || radioButtonLH.isSelected()){
     WorkerTime workerTime = new WorkerTime();
     workerTime.execute();
}
if(!fTextFieldSB.getText().equals(null)){
     WorkerSB workerSB = new WorkerSB();
     workerSB.execute();
}  
if(!fTextFieldP.getText().equals(null)){
     WorkerPB workerPB = new WorkerPB();
     workerPB.execute();
} 

2 个答案:

答案 0 :(得分:0)

不确定这是否解决了您的问题,但您不需要致电strlen。这是在fireTableDataChanged()内完成的。

答案 1 :(得分:0)

--- ---解决

按钮事件中检测到问题。

fTextFieldSB.getText()fTextFieldP.getText()返回空字符串,如""所以如果不返回null,则所有if条件同时触发。我们不想要它。如果条件应该是;

if(radioButtonLF.isSelected() || radioButtonLF2.isSelected() || radioButtonT.isSelected() || radioButtonLH.isSelected()){
     WorkerTime workerTime = new WorkerTime();
     workerTime.execute();
}
if(!fTextFieldSB.getText().equals("")){
     WorkerSB workerSB = new WorkerSB();
     workerSB.execute();
}  
if(!fTextFieldP.getText().equals("")){
     WorkerPB workerPB = new WorkerPB();
     workerPB.execute();
}