简体   繁体   English

Jtable行分类器导致IndexOutOfBoundsException

[英]Jtable row sorter causes IndexOutOfBoundsException

In my PhoneBook applcation after sorting by a column , when i remove a row and cal updateUI() i got a java.lang.IndexOutOfBoundsException in my model . 在我的PhoneBook applcation中按列排序后,当我删除一行并且cal updateUI() ,我的模型中出现了java.lang.IndexOutOfBoundsException But if not sorting there is no exeption I guess the object has removed but in updateUI procedure it doesnt know that and somewhere return old getRowCount() ,according to stacktrace. 但是如果没有排序那么没有exeption我猜对象已被删除但是在updateUI过程中它根本不知道并且在某处返回旧的getRowCount() ,根据stacktrace。

    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();
        }

here is stacktrace: 这是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)

and model.getvalueat: 和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;
}

Don't call updateUI() as this should only be called when L&F is changed. 不要调用updateUI()因为只有在更改L&F时才会调用它。 Your delete row method is part of your model right? 您的删除行方法是您的模型的一部分吗? Are you firing the model's fireXXX() notification methods after deleting? 你是否在删除后触发模型的fireXXX()通知方法? You should be. 你应该。 Also, I wonder if you should be using an iterator to do your deleting. 另外,我想知道你是否应该使用迭代器进行删除。


Edit 编辑
You state: 你说:

No delet method is part of my controller (is it wrong?). 没有delet方法是我的控制器的一部分(这是错的?)。

Wrong. 错误。 The method should be part of your table model, and controller can call this method on the model, but shouldn't have this method. 该方法应该是表模型的一部分,并且控制器可以在模型上调用此方法,但不应该使用此方法。 The table model should extend AbstractTableModel and should call the proper fireXXX method when data is removed, added, or changed. 表模型应该扩展AbstractTableModel,并且应该在删除,添加或更改数据时调用正确的fireXXX方法。 For delete, call fireTableRowsDeleted method, and definitely check the AbstractTableModel API for the details on all such available notification methods. 对于delete,请调用fireTableRowsDeleted方法,并检查AbstractTableModel API以获取有关所有此类可用通知方法的详细信息。

I removed 'updateUI()' line ,its ok until i click on a cell of table ,when i do this he exeption thrwon . 我删除了'updateUI()'行,它确定,直到我点击表格的单元格,当我这样做时,他执行了一下。 means that actually 'firexxx()' cuase it ,right? 意味着实际上'firexxx()'来吧,对吧?

No. I have no idea what your code is doing or the cause of your exceptions right now. 不,我现在不知道你的代码在做什么或者你的例外的原因。 Consider creating and posting an sscce . 考虑创建和发布sscce

Oh youre right . 哦,你是对的。 but Why when i call 'table.getModel()' i dont see fireXXX()'but by with a refernce to model instance it will be seen. 但为什么当我调用'table.getModel()'时,我看不到fireXXX()',但是通过引用模型实例,它将被看到。 'mymodel.fireTableDataChanged()' 'mymodel.fireTableDataChanged()'

Outside classes should not call the fire methods. 外面的课不应该叫火方法。 The model itself should be the only object calling its own notification methods. 模型本身应该是调用自己的通知方法的唯一对象。

If you haven't gone through the JTable tutorial, I suggest that you consider doing this without delay. 如果您还没有完成JTable教程,我建议您考虑立即执行此操作。 It will help you a great deal. 它会帮助你很多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM