Results 1 to 7 of 7
  1. #1
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default 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();  
            		      }  
            		   }  
            		);



    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); 
    	 }
    error:

    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)

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Column Sort on JTable outside selection out of bounds index exception

    Moved from New to Java

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default 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();  
            		      }  
            		   }  
            		);

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Column Sort on JTable outside selection out of bounds index exception

    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)
    1. What's on line 590?
    2. What does JTable#getSelectedRow() return when no row is selected? Where do you test for that?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Column Sort on JTable outside selection out of bounds index exception

    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++) {
    I'm not sure how to test for this. From the behavior I observe when I run the application:
    - 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.

  6. #6
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default Re: Column Sort on JTable outside selection out of bounds index exception

    ok, test printing values for row and selRows:

    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);
    These are printing what I expect..

  7. #7
    Redefine12 is offline Member
    Join Date
    Feb 2012
    Posts
    66
    Rep Power
    0

    Default 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-12-2012 at 12:12 AM.

Similar Threads

  1. array index out of bounds exception, AGAIN!
    By samanyu in forum New To Java
    Replies: 8
    Last Post: 06-08-2011, 12:27 PM
  2. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 11:24 PM
  3. Array Index Out Of Bounds Exception
    By manowar689 in forum New To Java
    Replies: 3
    Last Post: 06-19-2010, 12:25 AM
  4. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  5. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 08:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •