每次我尝试更新JTable中的数据时都会收到此错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 2
at javax.swing.DefaultRowSorter.convertRowIndexToModel(DefaultRowSorter.java:501)
at javax.swing.JTable.convertRowIndexToModel(JTable.java:2620)
at javax.swing.JTable.getValueAt(JTable.java:2695)
at javax.swing.JTable.prepareRenderer(JTable.java:5712)
at javax.swing.plaf.basic.BasicTableUI.paintCell(BasicTableUI.java:2069)
at javax.swing.plaf.basic.BasicTableUI.paintCells(BasicTableUI.java:1971)
at javax.swing.plaf.basic.BasicTableUI.paint(BasicTableUI.java:1767)
at javax.swing.plaf.ComponentUI.update(ComponentUI.java:143)
at javax.swing.JComponent.paintComponent(JComponent.java:751)
at javax.swing.JComponent.paint(JComponent.java:1017)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JViewport.paint(JViewport.java:747)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintChildren(JComponent.java:852)
at javax.swing.JComponent.paint(JComponent.java:1026)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5112)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1220)
at javax.swing.JComponent._paintImmediately(JComponent.java:5060)
at javax.swing.JComponent.paintImmediately(JComponent.java:4870)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:803)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:125)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
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)
数据似乎正在更新,但出现了问题,而且我甚至没有引用任何代码,这让我很烦恼。经过一段时间的调试,我确定以下几行代码导致了问题:
public class LoadingLogListThread extends Thread{
...
public void run() {
...
int colNo = rsmd.getColumnCount();
while(rs.next() ){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
if( objects != null )
aModel.addRow(objects);
count++;
}
mainView.logEntryTable.setModel(aModel);
...
}
当我单击按钮来过滤列表时创建并启动线程,然后返回到数据库以获取可能超出已经获取的数据的更多数据。我计划稍后确定是否需要新的提取,并且只在这种情况下执行上述操作,否则只过滤已经提取的结果。
答案 0 :(得分:0)
您需要在Event Dispatch Thread上更新模型。生成一个新线程来执行某些工作是正确的,特别是因为正在从数据库中提取数据。试试这个:
public class LoadingLogListThread extends Thread{
...
public void run() {
...
int colNo = rsmd.getColumnCount();
while(rs.next() ){
Object[] objects = new Object[colNo];
for(int i=0;i<colNo;i++){
objects[i]=rs.getObject(i+1);
}
if( objects != null )
aModel.addRow(objects);
count++;
}
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
mainView.logEntryTable.setModel(aModel);
}
});
...
}