• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 350
  • Last Modified:

Java remove Jtable rows according to content of row

Hello.

I am having a problem with a JTable. I have a multi-threaded server with users connecting to it; once a user is connected their details are put into a JTable.

The code attached below is run every time a user disconnects from the server (by closing their window or clicking 'disconnect'). It goes through each row and checks the ID column of the JTable against the ID of the user and deletes the user that just disconnected.

I am get the following errors which are a bit of a mystery to me:

Exception in thread "Thread-4" java.lang.ArrayIndexOutOfBoundsException: 2 >= 2
      at java.util.Vector.elementAt(Vector.java:432)
      at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:621)
      at lec.StuCo.run(StuCo.java:105)
Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
      at java.util.Vector.elementAt(Vector.java:432)
      at javax.swing.table.DefaultTableModel.getValueAt(DefaultTableModel.java:621)
      at lec.StuCo.run(StuCo.java:105)

The code *seems* to be working correctly but I think it might be something to do with each thread calling removeRow or getValueAt at the same time as it's threaded?

If you could help at all it would be very helpful.
int i;
int rc = BuildGUI.theModel.getRowCount();
 
for (i=0; i<rc; i++ ) {
	int rowid = Integer.parseInt((String)DrawGUI.tmodel.getValueAt(i,0));
	int theid = Integer.parseInt(details[0]);
	System.out.println("rowid = " +rowid);
	System.out.println("theid = : " +theid);
	System.out.println("if( " +rowid + " == " + theid + ")");
	if(rowid==theid) {
		System.out.println("the row to be deleted is: " +i);
		DrawGUI.tmodel.removeRow(i);
	}
}

Open in new window

0
maxlostyourdog
Asked:
maxlostyourdog
  • 3
  • 2
1 Solution
 
CEHJCommented:
You're changing the index in mid-iteration. Use Iterator.remove instead in conjunction with the index. You could also try the for loop backwards but the former solution is better
0
 
objectsCommented:
try this:

for (i=0; i
0
 
objectsCommented:
or:

for (i=0; i=0; i++ ) {
        int rowid = Integer.parseInt((String)DrawGUI.tmodel.getValueAt(i,0));
        int theid = Integer.parseInt(details[0]);
        System.out.println("rowid = " +rowid);
        System.out.println("theid = : " +theid);
        System.out.println("if( " +rowid + " == " + theid + ")");
        if(rowid==theid) {
                System.out.println("the row to be deleted is: " +i);
                DrawGUI.tmodel.removeRow(i);
        }
}


take your pick :)

0
 
CEHJCommented:
>>
for (i=0; i<rc; i++ ) {
...
        if(rowid==theid) {
..
                i = rc;
        }
}

==============

for (i=0; i<rc; i++ ) {
...
        if(rowid==theid) {
..
                i--;
        }
}

>>

I really must post some links to this question, the above being almost an object lesson on how NOT to write for loops. Maybe someone else can throw in a couple of labels and break statements perhaps? ;-)
0
 
CEHJCommented:
:-)
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now