java - How to sort JTable rows if Override "getColumnClass" has no effect -


in fact, it's more answer question. have had issue tablerowsorter when creating jtable file. use tablemodel this:

public class tmodel extends abstracttablemodel {      public vector data;     public vector colnames;     public string datafile;     class[] types = {integer.class, date.class, number.class, string.class, character.class};      public tmodel(string f) {         datafile = f;         initvectors();     }      public void initvectors() {         string aline;         data = new vector();         colnames = new vector();         try {             fileinputstream fin = new fileinputstream(datafile);             bufferedreader br = new bufferedreader(new inputstreamreader(fin));             // extract column names             stringtokenizer st1 =                     new stringtokenizer(br.readline(), "|");             while (st1.hasmoretokens())                 colnames.addelement(st1.nexttoken());             // extract data             while ((aline = br.readline()) != null) {                 stringtokenizer st2 =                         new stringtokenizer(aline, "|");                 while (st2.hasmoretokens())                     data.addelement(st2.nexttoken());             }             br.close();         } catch (exception e) {             e.printstacktrace();         }     }      public int getrowcount() {         return data.size() / getcolumncount();     }      public int getcolumncount() {         return colnames.size();     }      public string getcolumnname(int columnindex) {         string colname = "";          if (columnindex <= getcolumncount())             colname = (string) colnames.elementat(columnindex);          return colname;     }      public class getcolumnclass(int columnindex) {         return this.types[columnindex];     }      public boolean iscelleditable(int rowindex, int columnindex) {         return false;     }      public object getvalueat(int rowindex, int columnindex) {         return data.elementat((rowindex * getcolumncount()) + columnindex);     }      public void setvalueat(object avalue, int rowindex, int columnindex) {         return;     } 

the issue first column in table sorted string regardless type returns "getcolumnclass" method. popular recommendations such problem are: "you should override getcolumnclass method" or "set specific type each column".. sadly, none of works me :_( this topic helped me lot, snippet alanmars. simple soution: if tablerowsorter treats numbers string, make compare them integer! below - snippet, works me:

tmodel tmodel = new tmodel(selectedfile); jtable table = new jtable(); jscrollpane scrollpane = new jscrollpane(table, scrollpaneconstants.vertical_scrollbar_as_needed, scrollpaneconstants.horizontal_scrollbar_as_needed); sorter = new tablerowsorter<>(tmodel); sorter.setcomparator(indexofdesiredcolumn, new comparator<string>() {     @override     public int compare(string n1, string n2) {     return integer.parseint(n1) - integer.parseint(n2);     } }); table.setmodel(tmodel); table.setrowsorter(sorter); 

hope tread save time who'll face same problem me.

all of columns data raw vector data in object...

and sorter takes objects, not null, , calls tostring() on them, , sorting result...

your getcolumnclass() method not implemented right, if there no data in model (eg empty) throw nullpointerexception...


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -