i trying collect data jtable using method below array.
public string[][] gettabledata (jtable table) { defaulttablemodel dtm = (defaulttablemodel) table.getmodel(); int nrow = dtm.getrowcount(), ncol = dtm.getcolumncount(); string[][] tabledata = new string[nrow][ncol]; (int = 0 ; < nrow ; i++) (int j = 0 ; j < ncol ; j++) tabledata[i][j] = (string) dtm.getvalueat(i,j); return tabledata; }
everything works fine not able data of last row last column. no error given. when change loop range, arrayoutbound error.
the method returns below:
public void setgrid(string data [][], string header []){ dtm.setrowcount(0); dtm.setcolumncount(0); dtm.setdatavector(data, header); tbl1.getcolumnmodel().getcolumn(0).setpreferredwidth(25); }
i have edited method below data captured.
public string[][] gettabledata (jtable table) { string dat= new string(); defaulttablemodel dtm = (defaulttablemodel) table.getmodel(); int nrow = dtm.getrowcount(), ncol = dtm.getcolumncount(); string[][] tabledata = new string[nrow][ncol]; (int = 0 ; < nrow ; i++) (int j = 0 ; j < ncol ; j++) dat=dat + (string) dtm.getvalueat(i,j); bb.toplabel.settext("data :" +dat); //tabledata[i][j] = (string) dtm.getvalueat(i,j); return tabledata; }
the data displayed null though cell has value.
you had right forgot store dat in array. casting object string doesn't make string. better way string tostring() method or treat object string in other operator plus(+). here right , simplified code:
public string[][] gettabledata (jtable table) { defaulttablemodel dtm = (defaulttablemodel) table.getmodel(); int nrow = dtm.getrowcount(), ncol = dtm.getcolumncount(); string[][] tabledata = new string[nrow][ncol]; (int = 0 ; < nrow ; i++) (int j = 0 ; j < ncol ; j++) { string dat=""+ dtm.getvalueat(i,j);// string bb.toplabel.settext("data :" +dat); tabledata[i][j] = dat; } return tabledata; }
one more thing - use curly braces in code. 1 line in loop. avoid errors when add more lines of code loop body.