File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Out of bound on removeRow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Out of bound on removeRow" Watch "Out of bound on removeRow" New topic

Out of bound on removeRow

Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Still working through the table issues, but learning.

I am trying to implement removing a row from the table. I click on the row, then click on a button to remove the row. The action listener is called, and the model can be referenced, but when I issue the removeRow() method I get an Out Of Bounds exception, even though the row number being removed does exist. I just can't seem to see what I am doing wrong with this.

I've tried selecting the first and second rows, get the same errors regardless:

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\pmll42>cd desktop\java\test

Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10815

Hint: After you remove the 1st selected row, think what happens to the row count.

[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Maneesh Godbole wrote:Hint: After you remove the 1st selected row, think what happens to the row count.

I don't know. I put a println after the removeRow just to check that (I had the thought that I was looping and removing more than the two rows), the println was never executed. Apparently the exception is being thrown on the removeRow command itself.
pete stein

Joined: Feb 23, 2007
Posts: 1561
Are you sure that you don't want to use an AbstractTableModel and not a DefaultTableModel here? The DTM comes with its own Vector of Vectors to hold the data, and as you are using it, currently this object is empty, so the DTM sees no rows. If in fact you want to use your own array as the model, then I think you're better off with the ATM.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2446
I always recommend you start by using the DefaultTableModel because all the methods and functionality is implemented for you. Once you understand how models work then you can create a custom model if you feel it is necessary.

So start with the DefaultTableModel. The only method you need to override is the getColumnClass() method. You can create a DefaultTableModel by using a 2D array. The values in the Array will be copied to the DefaultTableModel so the model can reference the data. Now any changes to the data MUST be done by using the model.

When you delete multiple row you delete the last row first. That is instead of deleting rows 0, 1, 2, you delete rows 2, 1, 0. That way to don't try to delete a row that doesn't exist.
Mike Lipay
Ranch Hand

Joined: Sep 11, 2007
Posts: 171
Pete, Rob,

Thanks, this is going to take some re-working of the code, but I think you are right. Should have waded in rather than jumping into the deep end first.
I agree. Here's the link:
subject: Out of bound on removeRow
It's not a secret anymore!