3

I have a JTable and a TableModel that extends AbstractTableModel. I would like to dynamically set the number of columns in the table. I implemented this by adding an attribute to my TableModel named, column_count, and having getColumnCount return the column_count. I also added a method, setColumnCount, that sets column_count and calls fireTableStructureChanged. Unfortunately, when I ran the program, I kept getting ArrayIndexOutOfBounds exceptions. Can anyone tell me what I did wrong, or suggest a better solution?

Here's a stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 4 >= 4 at java.util.Vector.elementAt(Unknown Source) at javax.swing.table.DefaultTableColumnModel.getColumn(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paintGrid(Unknown Source) at javax.swing.plaf.basic.BasicTableUI.paint(Unknown Source) at javax.swing.plaf.ComponentUI.update(Unknown Source) at javax.swing.JComponent.paintComponent(Unknown Source) at javax.swing.JComponent.paint(Unknown Source) at javax.swing.JComponent.paintToOffscreen(Unknown Source) at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source) at javax.swing.RepaintManager$PaintManager.paint(Unknown Source) at javax.swing.RepaintManager.paint(Unknown Source) at javax.swing.JComponent._paintImmediately(Unknown Source) at javax.swing.JComponent.paintImmediately(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source) at javax.swing.RepaintManager.seqPaintDirtyRegions(Unknown Source) at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(Unknown Source) at java.awt.event.InvocationEvent.dispatch(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

cesar
  • 8,944
  • 12
  • 46
  • 59
  • perhaps you can add some code for us to see? – Dan Dec 07 '10 at 14:15
  • What method is giving you the ArrayIndexOutOfBounds? Can you print a stack trace? – Pace Dec 07 '10 at 14:15
  • 2
    Can you extend `DefaultTableModel`? It already has `addColumn` method. – khachik Dec 07 '10 at 14:16
  • @Dan: what kind of code would be helpful? I don't want to bother others about posting a wall of text. – cesar Dec 07 '10 at 19:24
  • @khachik, I guess I could, but I usually prefer using `AbstractTableModel` and this is not part of something bigger, the whole point of this is to make a program capable of handling dynamic column counts. – cesar Dec 07 '10 at 19:27

3 Answers3

0

Calling to method setModel(tm) of JTable with tm the changed TableModel solved a similar error in my case.

RafaelCaballero
  • 1,555
  • 2
  • 17
  • 24
0

Where does your getValueAt() method takes data? If it is an ArrayList and you increase column size, than the table will try to fetch that column from the list and throw an exception.

If that is not the problem, either use DefaultTableModel and DefaultTableModel.addColumn() to add columns, or make sure that you make any changes to the table model from the Event Dispatch Thread.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68
  • `getValueAt()` uses the formula `(arg0 + 1) * arg1` and I haven't directly messed with the Event Dispatch Thread. All my test application does is create a GUI with a table based on the model, both of which are housed in a 'JScrollPane`. My main function instantiates the GUI and enters a while loop that increments the column count by 1 every 5 seconds. – cesar Dec 07 '10 at 19:22
0

I think the exception is caused as follows:

  1. You set internal column_count to +1
  2. You cause events to be fired which will cause the table to be updated visually
  3. When the JTable update code accesses the last column the internal Vector in the column model throws the exception.

The reason is probably because the underlying code of DefaultTableColumnModel does not know about the new column, and its Vector has not properly been altered.

To fix this you should probably write your own custom TableColumnModel which can deal with changing dimensions properly.

user268396
  • 11,576
  • 2
  • 31
  • 26