Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / March 2007

Tip: Looking for answers? Try searching our database.

JTable calls setValueAt with index out of bounds

Thread view: 
Lionel van den Berg - 18 Mar 2007 10:37 GMT
I'll start with minimal information in case this has a simple solution

I've got a JTable, table, and I've sub-classed AbstractTableModel and
set the JTable model to an instance of my sub-class. My model just has
an ArrayList, dataList, that holds the data types that represent each
row. My getRowCount() method in my model simply returns dataList.size().

I've got a delete button that calls:
((AbstractTableModel)table.getModel()).deleteRow(table.getSelectedRow());

The deleteRow(int row) method simply does:
dataList.remove(row);
fireTableRowsDeleted(row, row);

If I have a table with two entries, select the last entry and press
delete once it deletes the entry and there are no rows selected after it
is removed. If I press delete again my application catches this saying
that there is nothing selected (getSelectedRow() returns -1) but after
JTable seems to call model.setValueAt(...) with a row index of 1 which
is out of bounds. Of course this causes an IndexOutOfBoundsException
when it happens.

Why is this method being called with this illegal value? The only
solution I've found is to call:

table.setEditingRow(Table.getRowCount() -1);

But I don't like this, it seems like a hack.

Any ideas?
Michael Rauscher - 21 Mar 2007 13:05 GMT
Lionel van den Berg schrieb:
> I'll start with minimal information in case this has a simple solution

It sure has a simple solution but it seems that you didn't provide the
necessary information to find it :)

I've created an SSCCE for you (without the phenomenon you mentioned):

import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.table.*;

public class Test {

    static class StringListTableModel extends AbstractTableModel {
        private List<String> data;

        public StringListTableModel( List<String> data ) {
            this.data = data;
        }

        public int getColumnCount() { return 1; }
        public int getRowCount() { return data.size(); }
        public Object getValueAt( int row, int col ) {
            return data.get(row);
        }
        public boolean isCellEditable( int row, int col ) {
            return true;
        }
        public void setValueAt( Object value, int row, int col ) {
            data.set( row, (String)value );
            fireTableCellUpdated( row, col );
        }
        public void deleteRow( int row ) {
            data.remove( row );
            fireTableRowsDeleted( row, row );
        }
    }

    public static void main( String args[] ) throws Exception {
        final StringListTableModel model = new StringListTableModel(
                new ArrayList<String>(
                Arrays.asList("First", "Second", "Third")) );
        final JTable table = new JTable(model);

        JButton button = new JButton("Delete");
        button.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                int row = table.getSelectedRow();
                if ( row != -1 )
                    model.deleteRow( row );
                else
                    JOptionPane.showMessageDialog( null,
                            "No row selected." );
            }
        });

        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
        frame.add( new JScrollPane(table) );
        frame.add( button, java.awt.BorderLayout.SOUTH );
        frame.pack();
        frame.setVisible( true );
    }

}

Bye
Michael
Lionel van den Berg - 22 Mar 2007 08:24 GMT
> Lionel van den Berg schrieb:
>> I'll start with minimal information in case this has a simple solution
[quoted text clipped - 3 lines]
>
> I've created an SSCCE for you (without the phenomenon you mentioned):

Thanks. I'm going to have to look into this because at first glance the
only difference is the line:
fireTableCellUpdated( row, col );

I use fireTableDataChanged();

I'll try your test and see what happens.

Lionel.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.