0

i have a problem during adding rows to my tablemodel for my jxtable.

The following exception will be thrown:

Exception in thread "Thread-9" java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.checkAgainstModel(DefaultRowSorter.java:904)
at javax.swing.DefaultRowSorter.rowsInserted(DefaultRowSorter.java:844)
at javax.swing.JTable.notifySorter(JTable.java:4258)
at javax.swing.JTable.sortedTableChanged(JTable.java:4106)
at javax.swing.JTable.tableChanged(JTable.java:4383)
at org.jdesktop.swingx.JXTable.tableChanged(JXTable.java:1524)
at javax.swing.table.AbstractTableModel.fireTableChanged(AbstractTableModel.java:280)
at javax.swing.table.AbstractTableModel.fireTableRowsInserted(AbstractTableModel.java:215)
at javax.swing.table.DefaultTableModel.insertRow(DefaultTableModel.java:359)
at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:333)
at javax.swing.table.DefaultTableModel.addRow(DefaultTableModel.java:344)
at de.mudisar.MainWindow.addRow(MainWindow.java:2358)
at de.mudisar.dataloader.SelectionDataLoader.run(SelectionDataLoader.java:46)

I call the method which fills the model from a thread because that could be more then 10000 entries and otherwise my program will be freezed.

I am sorry that i can't paste the code but this is very critical in my company.

The exception comes at that line if i do:

infomodel.addRow(new Object[{1,2,3,4,5,6,7,8});

Have anybody an idea why this exception occurs?

With best

mKorbel
  • 109,525
  • 20
  • 134
  • 319
bladepit
  • 853
  • 5
  • 14
  • 29
  • 1
    for better help sooner post an [SSCCE](http://sscce.org/) – mKorbel Sep 26 '12 at 11:57
  • something wrong with the code you are not showing :-) Can only strengthen @mKorbel 's suggestion - show an SSCCE (be sure to read and understand the referenced article on how-to create such an example) – kleopatra Sep 26 '12 at 12:06
  • @kleopatra I would have thought the stacktrace was sufficient to you to provide a solution. – Robin Sep 26 '12 at 12:52
  • @Robin obviously, you overestimate me :-) Sigh, reading carefully probably would have helped me ... – kleopatra Sep 26 '12 at 13:49

2 Answers2

3
Exception in thread "Thread-9" java.lang.IndexOutOfBoundsException: Invalid range
at javax.swing.DefaultRowSorter.checkAgainstModel(DefaultRowSorter.java:904)
at javax.swing.DefaultRowSorter.rowsInserted(DefaultRowSorter.java:844)
at javax.swing.JTable.notifySorter(JTable.java:4258)

Thread-9 does not sound like the EDT. You shouldn't modify the model which is already placed on a table on another thread then the EDT. See the Concurrency in Swing tutorial for more information.

You could use e.g. SwingUtilities.invokeLater to schedule the update of the EDT. Or in case you have to re-populate your whole model, it might be easier to create a new TableModel on the worker thread, and replace the model in one go on the EDT

Robin
  • 36,233
  • 5
  • 47
  • 99
  • that you very much. you have give the solution. i added only one row in my thread and sometimes there comes the exception now i do this in a SwingUtilites.invokeLater method...thx – bladepit Sep 26 '12 at 13:51
  • @kleopatra you have full control over the source of the `JXTable` class. You could consider throwing simple `RuntimeException`s when the JXTable model is changed on a non-EDT thread, with a clear exception message (since Swing probably will never introduce thread checking in its core components/methods). The inexperienced Swing developer could only benefit from it – Robin Sep 26 '12 at 14:30
  • good point - consider to file an enhancement request in our issue tracker. Though not entirely sure how we could do it without breaking backwards compatibility: there are rare cases when the EDT violation doesn't throw up, adding a check which barks will break those. hmmm.. – kleopatra Sep 26 '12 at 14:38
  • @kleopatra I can think of several options: using asserts (you can run without them), behind a logger which first check whether it is debug/trace level, system property to disable. I will file the enhancement as soon as java.net emails me my password (of course I forgot my old one and switched PC's so its no longer stored) – Robin Sep 26 '12 at 14:49
  • my point (having been unclear, though) was more that all compatibility respecting approaches wouldn't help the newbie - they would have to be off by default. – kleopatra Sep 26 '12 at 15:02
  • @kleopatra I disagree. If you can turn those checks off but they are on by default, I think this is backwards compatible enough. The number of situations where it is safe to modify outside the EDT is so small that those people deserve the small extra effort in the upgrade – Robin Sep 26 '12 at 15:05
1

Maybe you add too many columns to a row - more than there are in model.

Daniel Cisek
  • 931
  • 3
  • 9
  • 23
  • good guess, but not quite correct: DefaultTableModel protects itself against column count mismatch :-) – kleopatra Sep 26 '12 at 12:08
  • I found similar question - http://stackoverflow.com/questions/6165060/after-adding-a-tablerowsorter-adding-values-to-model-cause-java-lang-indexoutofb – Daniel Cisek Sep 26 '12 at 12:17