Results 1 to 7 of 7
- 04-11-2012, 06:06 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Column Sort on JTable outside selection out of bounds index exception
I've got a gui including text fields and a jtable. I've added a sort function that takes values from the text fields. I've also included a mouse listener to take get the row values from the jtable to display in a detail tab. If the row selection contains different values from the parameters of the sorter, I receive error. I.e. if the row selected isn't returned by the sort parameter. I'll post code and error below. Is there a way to get around this, even if it's just clearing the mouse row selection? Or setting it to the first row of the sort selection? Where would you implement a work-around? I've seen that this is a documented bug in java.
Java Code://Mouse Event Listener for Row Selection into Detail Tab Object o; Object[] dtlist = new Object[53]; public void valueChanged(ListSelectionEvent e) { int maxRows = 0; Object value; if (!e.getValueIsAdjusting()) { selRows = table.getSelectedRow(); row = selRows; row = table.convertRowIndexToModel(row); // get Table data TableModel tm = table.getModel(); if (row > -1) { for (int i= 1; i < 53 ; i++) { value = tm.getValueAt(row,i); dtlist[i-1] = value; //Test-print Values retrieved by mouse listener //System.out.println("Selection : " + value ); } //get test value in array dtlist o = Array.get(dtlist, 1); System.out.println("o: "+o); //Key Fields dtECN.setText((String) Array.get(dtlist, 4)); dtCust.setText((String) Array.get(dtlist, 1)); dtMod.setText((String) Array.get(dtlist, 2)); dtSer.setText((String) Array.get(dtlist, 3)); dtNom.setText((String) Array.get(dtlist, 5)); //etc
Java Code://sf is the text field sf.getDocument().addDocumentListener ( new DocumentListener() { public void changedUpdate(DocumentEvent e) { newFilter(); } public void insertUpdate(DocumentEvent e) { newFilter(); } public void removeUpdate(DocumentEvent e) { newFilter(); } } );
error:Java Code:private void newFilter() { RowFilter<TableModel, Object> firstFilter = null; RowFilter<TableModel, Object> secondFilter = null; List<RowFilter<TableModel,Object>> filters = new ArrayList<RowFilter<TableModel,Object>>(); RowFilter<TableModel, Object> compoundRowFilter = null; try { firstFilter = RowFilter.regexFilter("^" + sf.getText(), 1); secondFilter = RowFilter.regexFilter("^" +sf2.getText(), 2); filters.add(firstFilter); filters.add(secondFilter); compoundRowFilter = RowFilter.andFilter(filters); // you may also choose the OR filter } catch (java.util.regex.PatternSyntaxException e) { return; } sorter.setRowFilter(compoundRowFilter); }
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at javax.swing.DefaultRowSorter.convertRowIndexToMode l(Unknown Source)
at javax.swing.JTable.convertRowIndexToModel(Unknown Source)
at SearchTest.valueChanged(SearchTest.java:590)
at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
at javax.swing.DefaultListSelectionModel.fireValueCha nged(Unknown Source)
at javax.swing.DefaultListSelectionModel.setValueIsAd justing(Unknown Source)
at javax.swing.JTable$SortManager.restoreSelection(Un known Source)
at javax.swing.JTable$SortManager.processChange(Unkno wn Source)
at javax.swing.JTable.sortedTableChanged(Unknown Source)
at javax.swing.JTable.sorterChanged(Unknown Source)
at javax.swing.RowSorter.fireRowSorterChanged(Unknown Source)
at javax.swing.RowSorter.fireRowSorterChanged(Unknown Source)
at javax.swing.DefaultRowSorter.sort(Unknown Source)
at javax.swing.DefaultRowSorter.setRowFilter(Unknown Source)
at SearchTest.newFilter(SearchTest.java:458)
at SearchTest.access$0(SearchTest.java:442)
at SearchTest$3.insertUpdate(SearchTest.java:178)
at javax.swing.text.Abstractdocument.fireInsertUpdate (Unknown Source)
at javax.swing.text.Abstractdocument.handleInsertStri ng(Unknown Source)
at javax.swing.text.Abstractdocument.insertString(Unk nown Source)
at javax.swing.text.Plaindocument.insertString(Unknow n Source)
at javax.swing.text.Abstractdocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.replaceSelection(U nknown Source)
at javax.swing.text.DefaultEditorKit$DefaultKeyTypedA ction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unkn own Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEv ent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKe yEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAsse rtions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent (Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectio nPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
- 04-11-2012, 06:51 PM #2
Re: Column Sort on JTable outside selection out of bounds index exception
Moved from New to Java
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-11-2012, 09:03 PM #3
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: Column Sort on JTable outside selection out of bounds index exception
Thank you db.
I've tried adjusting the document listener to clear selection values and am still receiving the error. Feeling very lost as to how to proceed with this problem.
Java Code:sf.getDocument().addDocumentListener ( new DocumentListener() { public void changedUpdate(DocumentEvent e) { //table.removeRowSelectionInterval(selRows, selRows); //table.removeRowSelectionInterval(row, row); if(selRows > 1 || row > 1){ table.clearSelection(); } newFilter(); } public void insertUpdate(DocumentEvent e) { //table.removeRowSelectionInterval(selRows, selRows); //table.removeRowSelectionInterval(row, row); if(selRows > 1 || row > 1){ table.clearSelection(); } newFilter(); } public void removeUpdate(DocumentEvent e) { //table.removeRowSelectionInterval(selRows, selRows); //table.removeRowSelectionInterval(row, row); if(selRows > 1 || row > 1){ table.clearSelection(); } newFilter(); } } );
- 04-11-2012, 09:32 PM #4
Re: Column Sort on JTable outside selection out of bounds index exception
1. What's on line 590?Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: -1
at javax.swing.DefaultRowSorter.convertRowIndexToMode l(Unknown Source)
at javax.swing.JTable.convertRowIndexToModel(Unknown Source)
at SearchTest.valueChanged(SearchTest.java:590)
2. What does JTable#getSelectedRow() return when no row is selected? Where do you test for that?
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 04-11-2012, 09:53 PM #5
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: Column Sort on JTable outside selection out of bounds index exception
I'm not sure how to test for this. From the behavior I observe when I run the application:Java Code:public void valueChanged(ListSelectionEvent e) { Object value; if (!e.getValueIsAdjusting()) { selRows = table.getSelectedRow(); // <<line 590 row = selRows; row = table.convertRowIndexToModel(row); // get Table data TableModel tm = table.getModel(); if (row > -1) { for (int i= 1; i < 53 ; i++) {
- If a row is NOT selected, the the sort does not cause an error.
- If the row selected is not within the parameters defined by the sort, an error results.
The angle I'm looking at now is removing the selection prior to begining a sort using table.clearSelection(); I've tried various iterations of this, altering the selection model, etc all to no avail.
- 04-11-2012, 10:02 PM #6
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: Column Sort on JTable outside selection out of bounds index exception
ok, test printing values for row and selRows:
These are printing what I expect..Java Code:Object o; Object[] dtlist = new Object[53]; public void valueChanged(ListSelectionEvent e) { Object value; if (!e.getValueIsAdjusting()) { selRows = table.getSelectedRow(); row = selRows; row = table.convertRowIndexToModel(row); // get Table data TableModel tm = table.getModel(); if (row > -1) { for (int i= 1; i < 53 ; i++) { value = tm.getValueAt(row,i); dtlist[i-1] = value; //Test-print Values retrieved by mouse listener //System.out.println("Selection : " + value ); } //get test value in array dtlist o = Array.get(dtlist, 1); System.out.println("o: "+o); System.out.println(selRows); System.out.println(row);
- 04-11-2012, 10:31 PM #7
Member
- Join Date
- Feb 2012
- Posts
- 66
- Rep Power
- 0
Re: Column Sort on JTable outside selection out of bounds index exception
Sorry for all the posts but this is driving me crazy and I'm very anxious to solve. I believe its failing on
row = table.convertRowIndexToModel(row);
I implemented this to preseve integrity of the detail data view when the rows are sorted. I.e. without rowindextomodel, the detail data will show a different record after sorting because the clicked row index in the VIEW is not the index of the record in the actual table. I commented out this portion and I don't recieve the error, but now, as predicted, the clicked row is not the row in the original data model. I can't compromise either!
From the Oracle docs: "The previous example assumes you have not enabled filtering. If you have enabled filtering convertRowIndexToView will return -1 for locations that are not visible in the view. "
This would explain the exception.. But how to get around this!?Last edited by Redefine12; 04-11-2012 at 11:12 PM.
Similar Threads
-
array index out of bounds exception, AGAIN!
By samanyu in forum New To JavaReplies: 8Last Post: 06-08-2011, 11:27 AM -
Help Array Index out of bounds exception
By star400040 in forum New To JavaReplies: 2Last Post: 12-10-2010, 10:24 PM -
Array Index Out Of Bounds Exception
By manowar689 in forum New To JavaReplies: 3Last Post: 06-18-2010, 11:25 PM -
array Index out of Bounds exception== 0
By Allgorythm in forum New To JavaReplies: 6Last Post: 02-11-2010, 04:02 PM -
Array Index Out of Bounds Exception
By kool001 in forum New To JavaReplies: 1Last Post: 12-03-2009, 07:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks