• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

add value at col,row in a 2d arraylist

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.



throws

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.add(ArrayList.java:367)
at tableModelDemo.MyTableModel.setValueAt(MyTableModel.java:80)
at tableModelDemo.TableDemo.<init>(TableDemo.java:35)
at tableModelDemo.TableDemo.createAndShowGUI(TableDemo.java:57)
at tableModelDemo.TableDemo.access$0(TableDemo.java:48)
at tableModelDemo.TableDemo$1.run(TableDemo.java:71)



I don't see the error. Please help. Here is the class with the relevant methods:

 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, you have no elements in your array. Then you try to set the 1st element. You check that the size is big enough. It's not. So you add a new element to the array. The problem is that the first element index is 0, and you are trying to access it with the "row" which is 1. Shouldn't you do model.setValueAt("hello", 0, 0); instead ? (or correct ensureCapacity to make enough rows (i.e. looping from content2Darray.size() to row included))
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're ensuring that there is an ArrayList for the row, but not that there are enough elements in this row. Yes, you are calling content2Darray.get(row).ensureCapacity(num), but that only makes sure there is room in the backing array to store enough elements without needing to resize (and frankly, 1 too few; col == 1 means there must be space for two elements). It doesn't change the size, or add elements.

I'd change it slightly:
Two final notes:
- you forgot the fireTableStructureChanged() call in ensureCapacity(row, num)
- either call fireTableStructureChanged() once, at the end (and only if needed), or call the proper method: fireTableRowsInserted

There are still other problems here though: getValueAt may also throw exceptions, because you may be trying to access elements that do not exist. Here too you must call ensureCapacity(row, col + 1) first.
 
Matt Kurz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello.

First: Thanks for your help. I have still some questions.

Rob Prime wrote:
col == 1 means there must be space for two elements). It doesn't change the size, or add elements.


Why?
You mean this code line:



Rob Prime wrote:
- you forgot the fireTableStructureChanged() call in ensureCapacity(row, num)


I tried several variations, without succes. The table is always empty.

Rob Prime wrote:
- either call fireTableStructureChanged() once, at the end (and only if needed), or call the proper method: fireTableRowsInserted


How can I do it only if needed?

Rob Prime wrote:
There are still other problems here though: getValueAt may also throw exceptions, because you may be trying to access elements that do not exist. Here too you must call ensureCapacity(row, col + 1) first.



Ok. I prefer to check for existance.


Does somebody have a complete example for a TableModel with a 2d ArraylList? It would help to see, how it's right. Or a good link with much help informations? I already know the SUN tutorial, but it ends, where I begin.

Here is a self running example of my current code:
 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would make sense to me that you would first want to add columns (so your column count isn't 0) and rows (so your ArrayLists can accept data before setting values.
 
Matt Kurz
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

pete stein wrote:It would make sense to me that you would first want to add columns (so your column count isn't 0) and rows (so your ArrayLists can accept data before setting values.



I see what you mean. JTable is a beast!

Now I makes some steps back and try to learn with the DefaultTableModel. I made an experiment:


The Output is:
columnmodel says nr colums is: 2
tablemodel says nr colums is : 4

The Gui views 2 columns.
Intresting is, that If I use setValueAt(), I have to do something like model.setColumnCount(4) first. It's not enough to add the columns to the ColumnModel. It seems like the developer must care both, if he wants to use the methods provided by the TableModel. That's all, but not fine.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic