嗨我使用第二个线程每2秒更新一次JTable的DefaultTableModel并且随意地随机,它会抛出以下错误。
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2 at java.util.Vector.elementAt(Vector.java:470) at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:649) at javax.swing.JTable.getValueAt(JTable.java:2716) at javax.swing.JTable.prepareRenderer(JTable.java:5714) at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2108) at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:2010) at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1806) at javax.swing.plaf.ComponentUI.update(ComponentUI.java:161) at javax.swing.JComponent.paintComponent(JComponent.java:769) at javax.swing.JComponent.paint(JComponent.java:1045) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JViewport.paint(JViewport.java:731) at javax.swing.JComponent.paintChildren(JComponent.java:878) at javax.swing.JComponent.paint(JComponent.java:1054) at javax.swing.JComponent.paintToOffscreen(JComponent.java:5212) at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:295) at javax.swing.RepaintManager.paint(RepaintManager.java:1236) at javax.swing.JComponent._paintImmediately(JComponent.java:5160) at javax.swing.JComponent.paintImmediately(JComponent.java:4971) at javax.swing.RepaintManager$3.run(RepaintManager.java:796) at javax.swing.RepaintManager$3.run(RepaintManager.java:784) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784) at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757) at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706) at javax.swing.RepaintManager.access$1000(RepaintManager.java:62) at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1651) at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:727) at java.awt.EventQueue.access$200(EventQueue.java:103) at java.awt.EventQueue$3.run(EventQueue.java:688) at java.awt.EventQueue$3.run(EventQueue.java:686) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:697) 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)
程序运行良好,但我不知道代码中的错误在哪里,因为我的类中没有一个出现在堆栈跟踪中。
答案 0 :(得分:7)
欢迎来到Event Dispatch Thread违规(以及竞争条件)的精彩世界。
基本上,您不应该(直接或间接)更新任何除EDT之外的任何线程的UI组件。
基本上,当您更新TableModel
时,它会触发一个事件,该事件已被表捕获,该事件正在尝试自行更新,但模型状态不断变化且对于表...
相反,尝试使用SwingWorker
更新模型,使用发布和处理方法使更新与EDT保持同步
查看Concurrency in Swing了解更多详情和示例。