« Return to Thread: IndexOutOfBoundsException on custom table implementation

Re: IndexOutOfBoundsException on custom table implementation

by Brian Merrill :: Rate this Message:

Reply to Author | View in Thread

Wingnut wrote:
>     public synchronized void listChanged(ListEvent<MyTableObject> listChanges) {
>    while (listChanges.next()) {
> List<MyTableObject> list = listChanges.getSourceList();
>
> int sourceIndex = listChanges.getIndex();
> int changeType = listChanges.getType();
>
> MyTableObject obj = list.get(sourceIndex);
>  
It looks to me like the line above is the source of your problems.  When
changeType is DELETE, the object at "sourceIndex" has already been
deleted from "list" and therefore you cannot access it.  You must
maintain a separate list of objects which mirrors the EventList in order
to obtain a reference to the deleted object.  However, it doesn't look
like you really need access to the deleted object so you may be able to
fix the probably by rearranging a few things and adding and "updateItem"
function in your NGridTableModel assuming that calling those functions
correctly updates a separate list:

public class NGridTableModel implements ListEventListener<MyTableObject>
{
    public NGridTableModel(NGridTable parent, Grid table, EventList<MyTableObject> source,
            TableFormat<MyTableObject> tableFormat) {
        source.addListEventListener(this);
    }

    public synchronized void listChanged(ListEvent<MyTableObject> listChanges) {
            List<MyTableObject> list = listChanges.getSourceList();
            while (listChanges.next()) {
                int sourceIndex = listChanges.getIndex();
                int changeType = listChanges.getType();

                switch (changeType) {
                    case ListEvent.DELETE:
                        removeItem(sourceIndex);
                        break;
                    case ListEvent.INSERT:
                        final MyTableObject insert = list.get(sourceIndex);
                        addItem(sourceIndex, insert);
                        break;
                    case ListEvent.UPDATE:
                        final MyTableObject update = list.get(sourceIndex);
                        updateItem(update, sourceIndex);
                        break;
                }
            }
            table.redraw();
    }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@...
For additional commands, e-mail: users-help@...

 « Return to Thread: IndexOutOfBoundsException on custom table implementation

LightInTheBox - Buy quality products at wholesale price!