• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Out of bound on removeRow

 
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hint: After you remove the 1st selected row, think what happens to the row count.
 
Mike Lipay
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

YMMV.
 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 171
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic