java - invalid range exception with setAutoCreateRowSorter() -


i getting invalid range exception jtable when use setautocreaterowsorter(). if remove setautocreaterowsorter, problem goes away, no longer able sort table. also, if try setting sorter directly:

mytable.setrowsorter(new tablerowsorter(model)); 

i still same error.

this close sscce think can convey issue.

import java.util.arraylist; import javax.swing.table.abstracttablemodel;  public class mytesttable extends javax.swing.jframe {      private mytestabstracttablemodel model = new mytestabstracttablemodel();     private int selectedrow = -1;       public void setlist(arraylist arraylist) {         model.setlist(arraylist);     }      public mytesttable() {         initcomponents();          mytable.setmodel(model);         mytable.setautocreaterowsorter(true);  // error here     }      /**      * method called within constructor initialize form.      * warning: not modify code. content of method      * regenerated form editor.      */     @suppresswarnings("unchecked")     // <editor-fold defaultstate="collapsed" desc="generated code">                               private void initcomponents() {          jscrollpane1 = new javax.swing.jscrollpane();         mytable = new javax.swing.jtable();          setdefaultcloseoperation(javax.swing.windowconstants.exit_on_close);          mytable.setmodel(new javax.swing.table.defaulttablemodel(             new object [][] {              },             new string [] {                 "select", "first name", "last name"             }         ) {             class[] types = new class [] {                 java.lang.boolean.class, java.lang.string.class, java.lang.string.class             };              public class getcolumnclass(int columnindex) {                 return types [columnindex];             }         });         jscrollpane1.setviewportview(mytable);          javax.swing.grouplayout layout = new  javax.swing.grouplayout(getcontentpane());         getcontentpane().setlayout(layout);         layout.sethorizontalgroup(  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)             .addgroup(layout.createsequentialgroup()                 .addgap(187, 187, 187)                 .addcomponent(jscrollpane1,  javax.swing.grouplayout.preferred_size,  javax.swing.grouplayout.default_size,  javax.swing.grouplayout.preferred_size)                 .addcontainergap(243, short.max_value))         );         layout.setverticalgroup(  layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)             .addgroup(layout.createsequentialgroup()                 .addgap(91, 91, 91)                 .addcomponent(jscrollpane1,  javax.swing.grouplayout.preferred_size,  javax.swing.grouplayout.default_size,  javax.swing.grouplayout.preferred_size)                 .addcontainergap(154, short.max_value))         );          pack();     }// </editor-fold>                              /**      * @param args command line arguments      */     public static void main(string args[]) {         arraylist<mytest> mytestlist = new arraylist<>();         mytest myentry = new mytest(false, "joe");         mytestlist.add(myentry);         mytesttable mytesttable = new mytesttable();         mytesttable.setlist(mytestlist);         mytesttable.setvisible(true);     }      // variables declaration - not modify                          private javax.swing.jscrollpane jscrollpane1;     private javax.swing.jtable mytable;     // end of variables declaration                         public class mytestabstracttablemodel extends abstracttablemodel {          private string[] columnnames = {"select", "first name"};          private arraylist<mytest> mytestlist = new arraylist<>();          public void setlist(arraylist<mytest> mylist) {             this.mytestlist = mylist;         }          @override         public int getcolumncount() {             return columnnames.length;         }          @override         public int getrowcount() {             return mytestlist.size();         }          @override         public string getcolumnname(int column) {             return columnnames[column];         }          @override         public object getvalueat(int row, int column) {             object value = null;             mytest mytest = mytestlist.get(row);              switch (column) {                 case 0:                     value = mytest.getselected();                     break;                 case 1:                     value = mytest.getfirstname();             }             return value;         }          @override         public class getcolumnclass(int c) {             return getvalueat(0, c).getclass();         }          @override         public boolean iscelleditable(int row, int col) {             return true;         }          @override             public void setvalueat(object value, int row, int column) {             mytest mytest = mytestlist.get(row);             switch (column) {                 case 0:                     if (value instanceof boolean)                         mytest.setselected((boolean)value);                     break;                 case 1:                     mytest.setfirstname((string)value);             }             firetablecellupdated(row, column);  // exception points here         }     }      public static class mytest {          private boolean selected;         private string firstname;          public mytest() {         }          public mytest(boolean selected, string firstname) {             this.selected = selected;             this.firstname = firstname;         }          public boolean getselected() {             return selected;         }          public void setselected(boolean selected) {             this.selected = selected;         }          public string getfirstname() {             return firstname;         }          public void setfirstname(string firstname) {             this.firstname = firstname;         }     }  } 

the problem occurs when click checkbox on table. close sscce think can convey issue. if take away sorter, problem not persist.

here error:

exception in thread "awt-eventqueue-0" java.lang.indexoutofboundsexception: invalid range     @ javax.swing.defaultrowsorter.rowsupdated(defaultrowsorter.java:896)     @ javax.swing.defaultrowsorter.rowsupdated(defaultrowsorter.java:915)     @ javax.swing.jtable.notifysorter(jtable.java:4264)     @ javax.swing.jtable.sortedtablechanged(jtable.java:4118)     @ javax.swing.jtable.tablechanged(jtable.java:4395)     @ javax.swing.table.abstracttablemodel.firetablechanged(abstracttablemodel.java:296)     @ javax.swing.table.abstracttablemodel.firetablecellupdated(abstracttablemodel.java:275)     @ mytesttable$mytestabstracttablemodel.setvalueat(mytesttable.java:151)     @ javax.swing.jtable.setvalueat(jtable.java:2741)     @ javax.swing.jtable.editingstopped(jtable.java:4723)     @ javax.swing.abstractcelleditor.fireeditingstopped(abstractcelleditor.java:141)     @ javax.swing.defaultcelleditor$editordelegate.stopcellediting(defaultcelleditor.java:368)     @ javax.swing.defaultcelleditor.stopcellediting(defaultcelleditor.java:233)     @ javax.swing.defaultcelleditor$editordelegate.actionperformed(defaultcelleditor.java:385)     @ javax.swing.abstractbutton.fireactionperformed(abstractbutton.java:2022)     @ javax.swing.abstractbutton$handler.actionperformed(abstractbutton.java:2348)     @ javax.swing.defaultbuttonmodel.fireactionperformed(defaultbuttonmodel.java:402)     @ javax.swing.jtogglebutton$togglebuttonmodel.setpressed(jtogglebutton.java:308)     @ javax.swing.plaf.basic.basicbuttonlistener.mousereleased(basicbuttonlistener.java:252)     @ java.awt.component.processmouseevent(component.java:6535)     @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3324)     @ java.awt.component.processevent(component.java:6300)     @ java.awt.container.processevent(container.java:2236)     @ java.awt.component.dispatcheventimpl(component.java:4891)     @ java.awt.container.dispatcheventimpl(container.java:2294)     @ java.awt.component.dispatchevent(component.java:4713)     @ javax.swing.plaf.basic.basictableui$handler.repostevent(basictableui.java:948)     @ javax.swing.plaf.basic.basictableui$handler.mousereleased(basictableui.java:1164)     @ java.awt.awteventmulticaster.mousereleased(awteventmulticaster.java:290)     @ java.awt.component.processmouseevent(component.java:6535)     @ javax.swing.jcomponent.processmouseevent(jcomponent.java:3324)     @ java.awt.component.processevent(component.java:6300)     @ java.awt.container.processevent(container.java:2236)     @ java.awt.component.dispatcheventimpl(component.java:4891)     @ java.awt.container.dispatcheventimpl(container.java:2294)     @ java.awt.component.dispatchevent(component.java:4713)     @ java.awt.lightweightdispatcher.retargetmouseevent(container.java:4888)     @ java.awt.lightweightdispatcher.processmouseevent(container.java:4525)     @ java.awt.lightweightdispatcher.dispatchevent(container.java:4466)     @ java.awt.container.dispatcheventimpl(container.java:2280)     @ java.awt.window.dispatcheventimpl(window.java:2750)     @ java.awt.component.dispatchevent(component.java:4713)     @ java.awt.eventqueue.dispatcheventimpl(eventqueue.java:758)     @ java.awt.eventqueue.access$500(eventqueue.java:97)     @ java.awt.eventqueue$3.run(eventqueue.java:709)     @ java.awt.eventqueue$3.run(eventqueue.java:703)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76)     @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:86)     @ java.awt.eventqueue$4.run(eventqueue.java:731)     @ java.awt.eventqueue$4.run(eventqueue.java:729)     @ java.security.accesscontroller.doprivileged(native method)     @ java.security.protectiondomain$javasecurityaccessimpl.dointersectionprivilege(protectiondomain.java:76)     @ java.awt.eventqueue.dispatchevent(eventqueue.java:728)     @ java.awt.eventdispatchthread.pumponeeventforfilters(eventdispatchthread.java:201)     @ java.awt.eventdispatchthread.pumpeventsforfilter(eventdispatchthread.java:116)     @ java.awt.eventdispatchthread.pumpeventsforhierarchy(eventdispatchthread.java:105)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:101)     @ java.awt.eventdispatchthread.pumpevents(eventdispatchthread.java:93)     @ java.awt.eventdispatchthread.run(eventdispatchthread.java:82) 

this part sticks out me:

at mytesttable$mytestabstracttablemodel.setvalueat(mytesttable.java:151) 

this post closest thing can find similar problem, not answered. how implement table sorter without receiving error?


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -