Question

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 . 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.

    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:

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:

@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;
}
Was it helpful?

Solution

Don't call updateUI() as this should only be called when L&F is changed. Your delete row method is part of your model right? Are you firing the model's fireXXX() notification methods after deleting? 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?).

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. For delete, call fireTableRowsDeleted method, and definitely check the AbstractTableModel API for the details on all such available notification methods.

I removed 'updateUI()' line ,its ok until i click on a cell of table ,when i do this he exeption thrwon . means that actually 'firexxx()' cuase it ,right?

No. I have no idea what your code is doing or the cause of your exceptions right now. Consider creating and posting an 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. '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. It will help you a great deal.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top