java - AbstractTableModel with ArrayList and CheckBoxes has setValueAt error -


i receive java.lang.indexoutofboundsexception: invalid range error abstracttablemodel setting display contents of arraylist. think has auto sorting table. how fix setautorowsorter()?

i have class 'account' holds arraylist of class 'client'.

the client class contains information displayed on table:

the table created drag-and-drop. create quick account , client here , make short list table:

package presentation;  import domain.account; import domain.client;  public class clienttable extends javax.swing.jframe {      private account account = null;     private clientabstracttablemodel model = new clientabstracttablemodel();     private int selectedrow = -1;      public void setaccount(account account) {         this.account = account;         model.setclients(account.getclientlist());     }      public clienttable() {         initcomponents();          mytable.setmodel(model);         mytable.setautocreaterowsorter(true);     }      /**      * 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[]) {         account account = new account();         client clienta = new client(false, "john", "smith", "new york", "current");         client clientb = new client(false, "jane", "doe", "london", "prospective");         account.addclient(clienta);         account.addclient(clientb);         clienttable clienttable = new clienttable();         clienttable.setaccount(account);         clienttable.setvisible(true);     }      // variables declaration - not modify                          private javax.swing.jscrollpane jscrollpane1;     private javax.swing.jtable mytable;     // end of variables declaration                    } 

and here abstracttablemodel:

package presentation;  import domain.client; import java.util.arraylist; import javax.swing.table.abstracttablemodel;  public class clientabstracttablemodel extends abstracttablemodel {      private string[] columnnames = {"select", "first name", "last name", "city", "status"};      private arraylist<client> clients = new arraylist<>();      public void setclients(arraylist<client> clients) {         this.clients = clients;     }      @override     public int getcolumncount() {         return columnnames.length;     }      @override     public int getrowcount() {         return clients.size();     }      @override     public string getcolumnname(int column) {         return columnnames[column];     }      @override     public object getvalueat(int row, int column) {         object value = null;         client client = clients.get(row);          switch (column) {             case 0:                 value = client.getselected();                 break;             case 1:                 value = client.getfirstname();                 break;             case 2:                 value = client.getlastname();                 break;             case 3:                 value = client.getcity();                 break;             case 4:                 value = client.getstatus();                 break;         }         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) {         client client = clients.get(row);         switch (column) {             case 0:                 if (value instanceof boolean)                     client.setselected((boolean)value);                 break;             case 1:                 client.setfirstname((string)value);                 break;             case 2:                 client.setlastname((string)value);                 break;             case 3:                 client.setcity((string)value);                 break;             case 4:                 client.setstatus((string)value);                 break;         }         firetablecellupdated(row, column);     } } 

the error occurs when click 1 of checkboxes. after click one, no longer able click others, can keep clicking first 1 clicked.

in exception details noticed this:

at presentation.clientlisttablemodel.setvalueat(clientlisttablemodel.java:152) 

which firetablecellupdated(row, column); in setvalueat() method.

interestingly, when remove

mytable.setautocreaterowsorter(true); 

from code, program works without errors. however, how around this? still able sort table clicking 1 of column headers.

for reference, got information on how set table this post , this java documentation.

thanks!

edit: removed unnecessary code snippets.


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 -