在按列排序后的PhoneBook应用中,当我删除一行并计算updateUI()
时,我在模型中得到了java.lang.IndexOutOfBoundsException
。但是,如果不分拣,就没有证据
我猜该对象已删除,但在updateUI过程中,它不知道该对象,并根据stacktrace在某处返回了旧的getRowCount()
。
private void delete(int[] selectedIndexes) {
ArrayList<Contact> arlDeleting = new ArrayList<Contact>();
for (int i = selectedIndexes.length - 1; i >= 0; i--) {
int realIndex = tblPhonebook.convertRowIndexToModel(selectedIndexes[i]);
tblMdlAllContacts.getData().remove(realIndex);
}
tblPhonebook.updateUI();
}
这是stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at com.TableModelPhoneBook.getValueAt(TableModelPhoneBook.java:73) ***
at javax.swing.JTable.getValueAt(Unknown Source)
at javax.swing.JTable.prepareRenderer(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCell(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paintCells(Unknown Source)
at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) *** i think getRowCount called here
at javax.swing.plaf.ComponentUI.update(Unknown Source)
at javax.swing.JComponent.paintComponent(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JViewport.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent._paintImmediately(Unknown Source)
at javax.swing.JComponent.paintImmediately(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$700(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(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)
和model.getvalueat:
@Override
public Object getValueAt(int rowIndex, int columnIndex) {
Contact temp = data.get(rowIndex); // here is where error occurs
switch (columnIndex) {
case 0:
return temp.getFirstName();
case 1:
return temp.getLastName();
case 2:
return temp.getMobile();
case 3:
return temp.getHome();
case 4:
return temp.getAddress();
default:
break;
}
return null;
}
最佳答案
不要调用updateUI()
,因为只有在更改L&F时才应调用。您的删除行方法是模型的一部分,对吗?删除后是否触发模型的fireXXX()
通知方法?你应该。另外,我想知道您是否应该使用迭代器进行删除。
编辑
您声明:
没有删除方法是我的控制器的一部分(错吗?)。
错误。该方法应该是表模型的一部分,并且控制器可以在模型上调用此方法,但不应具有此方法。表模型应该扩展AbstractTableModel,并且在删除,添加或更改数据时应调用适当的fireXXX方法。对于删除,请调用fireTableRowsDeleted方法,并绝对检查AbstractTableModel API以获取有关所有此类可用通知方法的详细信息。
我删除了“ updateUI()”这一行,直到我单击表格的一个单元格后,它才能正常运行,直到他点击退出。表示实际上是“ firexxx()”引起的,对吧?
不,我不知道您的代码在做什么或您的异常原因。考虑创建并发布sscce。
哦,对了。但是为什么当我调用'table.getModel()'时却看不到fireXXX()',而是通过引用模型实例可以看到它。 'mymodel.fireTableDataChanged()'
外部类不应调用fire方法。模型本身应该是调用其自己的通知方法的唯一对象。
如果您还没有完成JTable教程,建议您立即考虑这样做。它将对您有很大帮助。