java - JTable row deleting error -


i'm writting small file browser. have small problem downloading files implementation. when try cancel download , delete list, error:

exception in thread "timer-2" java.lang.arrayindexoutofboundsexception: 2 >= 2     @ java.util.vector.elementat(unknown source)     @ javax.swing.table.defaulttablemodel.setvalueat(unknown source)     @ pkg_main.cls_downloader$1.run(cls_downloader.java:151)     @ java.util.timerthread.mainloop(unknown source)     @ java.util.timerthread.run(unknown source) 

and code:

download class:

public class cls_downloader extends thread {      private final string sdownload_folder = "descargas/";      private socket tsock;      private objectinputstream tobjinstream;     private objectoutputstream tobjoutstream;      private jtable ttable;     private int iindex;      private cls_transfertablemodel ttransfermodel;      private boolean bdownloaded;     private boolean bfailed;      private long ireadcount = 0;     private long ireadtotal = 0;     private long ltotalsize = 0;     private long lremainsize = 0;      private string sfilename;      private jprogressbar tprogressbar;      public cls_downloader(socket tsock, jtable ttable, int iindex) {         this.ttable = ttable;         this.iindex = iindex;         this.tsock = tsock;         this.bdownloaded = false;         this.bfailed = false;          this.ttransfermodel = (cls_transfertablemodel) ttable.getmodel();          try {             this.tobjoutstream = new objectoutputstream(                     this.tsock.getoutputstream());             this.tobjoutstream.flush();             this.tobjinstream = new objectinputstream(                     this.tsock.getinputstream());              this.start();         } catch (ioexception e) {             this.bfailed = true;             // todo auto-generated catch block             e.printstacktrace();         }     }      public jtable gettable() {         return ttable;     }      public void settable(jtable ttable) {         this.ttable = ttable;     }      public int getiindex() {         return iindex;     }      public void setiindex(int iindex) {         this.iindex = iindex;     }      public socket getsock() {         return tsock;     }      public void setsock(socket tsock) {         this.tsock = tsock;     }      public string getfilename() {         return sfilename;     }      public void setfilename(string sfilename) {         this.sfilename = sfilename;     }      public long gettotalsize() {         return ltotalsize;     }      public void settotalsize(long ltotalsize) {         this.ltotalsize = ltotalsize;     }      public boolean isdownloaded() {         return bdownloaded;     }      public void setdownloaded(boolean bdownloaded) {         this.bdownloaded = bdownloaded;     }      public boolean isfailed() {         return bfailed;     }      public void setfailed(boolean bfailed) {         this.bfailed = bfailed;     }      private void settablevalues() {         this.ttransfermodel.setvalueat(this.tsock.getinetaddress()                 .gethostaddress(), iindex, 0);         this.ttransfermodel.setvalueat(this.sfilename, iindex, 1);         this.ttransfermodel.setvalueat("descargando", iindex, 2);          this.tprogressbar = (jprogressbar) ttable.getmodel().getvalueat(                 this.iindex, 3);         this.tprogressbar.setstringpainted(true);          this.ttransfermodel.setvalueat(                 cls_functions.convertsize(this.lremainsize), iindex, 6);     }  private void downloadcomplete() {         this.tprogressbar.setvalue(100);         this.ttable.repaint();         this.ttransfermodel.setvalueat("completado", iindex, 2);         this.ttransfermodel.setvalueat(cls_functions.convertsize(ireadtotal),                 iindex, 4);         this.ttransfermodel.setvalueat(cls_functions.convertsize(lremainsize),                 iindex, 5);          this.bdownloaded = true;     }      timertask tupdatetable = new timertask() {         public void run() {             tprogressbar.setvalue((int) ((ireadtotal * 100) / ltotalsize));             ttable.repaint();             ttransfermodel.setvalueat(cls_functions.convertsize(ireadtotal),                     iindex, 4); //error line             ttransfermodel.setvalueat(cls_functions.convertsize(lremainsize),                     iindex, 5);         }     };      public void run() {         string sdata = null;         fileoutputstream tfileinstream = null;          byte[] barrret = new byte[2048];          try {             sdata = this.tobjinstream.readutf();              this.sfilename = new file(sdata.split("#")[0]).getname();             this.ltotalsize = long.parselong(sdata.split("#")[1]);             this.lremainsize = this.ltotalsize;              this.settablevalues();             new timer().scheduleatfixedrate(tupdatetable, 0, 1000);              tfileinstream = new fileoutputstream(sdownload_folder + this.sfilename);              while (this.lremainsize > 0) {                 this.ireadcount = tobjinstream.read(barrret);                 tfileinstream.write(barrret, 0, (int) this.ireadcount);                 tfileinstream.flush();                 this.lremainsize -= this.ireadcount;                 this.ireadtotal += this.ireadcount;                  thread.sleep(5);             }              this.tupdatetable.cancel();             this.downloadcomplete();             tfileinstream.close();             this.close();              system.out.println("listo");          } catch (exception e) {             // todo auto-generated catch block             try {                 tfileinstream.close();             } catch (ioexception e1) {                 // todo auto-generated catch block                 e1.printstacktrace();             }              this.close();             this.bfailed = true;             this.ttransfermodel.setvalueat("fallado", iindex, 2);             this.tupdatetable.cancel();         }     }      public void close() {         try {             this.tupdatetable.cancel();             this.tobjoutstream.close();             this.tobjinstream.close();             this.tsock.close();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }     } } 

downloads manager class:

public class cls_downloadmanager {      private static cls_downloadmanager tdownloadmanager;     private arraylist<cls_downloader> tdownloadlist = new arraylist<cls_downloader>();      public static cls_downloadmanager getinstance() {         if (tdownloadmanager == null)             tdownloadmanager = new cls_downloadmanager();          return tdownloadmanager;     }      public arraylist<cls_downloader> getdownloadlist() {         return tdownloadlist;     }      public void setdownloadlist(arraylist<cls_downloader> tdownloadlist) {         this.tdownloadlist = tdownloadlist;     }      public void adddownload(cls_downloader tdownload) {         this.tdownloadlist.add(tdownload);     }      public cls_downloader getdownload(int iindex) {         return this.tdownloadlist.get(iindex);     }      public void deletedownload(int iindex) {         this.tdownloadlist.remove(iindex);     }      public void deletedownload(cls_downloader tdownload) {         this.tdownloadlist.remove(tdownload);     }      public void canceldownload(int iindex) {         this.tdownloadlist.get(iindex).close();     }      public void cleandownloads() {         (int = this.tdownloadlist.size() - 1; >= 0; i--)             if (this.tdownloadlist.get(i).isdownloaded()                     || this.tdownloadlist.get(i).isfailed())                 this.tdownloadlist.remove(i);     }      public int getlistcount() {         return this.tdownloadlist.size();     } } 

download cancel function:

if (tbl_downloads.getselectedrow() > -1) {     cls_downloadmanager.getinstance().canceldownload(tbl_downloads.getselectedrow()); } 

thanks in advance!

your design mixing business objects (the cls_downloader) view (the jtable , table model).

the problem see here cls_downloader has reference table model and, more importantly it's index in it. on top of keep track of downloads in list inside cls_downloadmanager.

when update data (by cancelling download) becomes complicated keep in sync (the table model, cls_downloadmanager, indices in cls_downloader).

look model view controller architecture better way organize code. in yor case, model should cls_downloader, view should jtable , controller should cls_downloadmanager


Comments