java - Sort JTable by value not included in the JTable -


i've jtable shows array of data of type

string [] data = {"string1", "string2", "string3", "string4"}; 

but in jtable show first 3 items , fourth hidden user. sorter table in base of string4that not shown in jtable. read jtable tutorial , found out how use tablerowsorterand set comparator method setcomparator wants argument column order not available me.

could me solve task? thank

the trick is, include column in tablemodel, remove tablecolumnmodel

sorted

the "a" column randomly generated, sort in unpredictable manner

you can disable columns been sortable using sorter.setsortable(1, false);, used prevent user clicking column header , ruining "hidden" sort.

import java.awt.borderlayout; import java.awt.eventqueue; import java.awt.event.actionevent; import java.awt.event.actionlistener; import java.util.arraylist; import java.util.comparator; import java.util.list; import javax.swing.jbutton; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.rowsorter; import javax.swing.sortorder; import javax.swing.uimanager; import javax.swing.unsupportedlookandfeelexception; import javax.swing.table.abstracttablemodel; import javax.swing.table.tablerowsorter;  public class magicsort {      public static void main(string[] args) {         new magicsort();     }      public magicsort() {         eventqueue.invokelater(new runnable() {             @override             public void run() {                 try {                     uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());                 } catch (classnotfoundexception | instantiationexception | illegalaccessexception | unsupportedlookandfeelexception ex) {                     ex.printstacktrace();                 }                  jframe frame = new jframe("testing");                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.add(new testpane());                 frame.pack();                 frame.setlocationrelativeto(null);                 frame.setvisible(true);             }         });     }      public class testpane extends jpanel {          private sortorder order = sortorder.ascending;          public testpane() {             rowdatatablemodel model = new rowdatatablemodel();             tablerowsorter<rowdatatablemodel> sorter = new tablerowsorter<>(model);             sorter.setcomparator(0, new comparator<string>() {                 @override                 public int compare(string o1, string o2) {                     return o1.compareto(o2);                 }             });              jtable table = new jtable(model);             table.setrowsorter(sorter);             table.getcolumnmodel().removecolumn(table.getcolumn("a"));              setlayout(new borderlayout());             add(new jscrollpane(table));              jbutton change = new jbutton("change");             add(change, borderlayout.south);              change.addactionlistener(new actionlistener() {                 @override                 public void actionperformed(actionevent e) {                     sort(sorter);                 }             });              sort(sorter);         }          protected void sort(tablerowsorter<rowdatatablemodel> sorter) {              list<rowsorter.sortkey> keys = new arraylist<>(1);             keys.add(new rowsorter.sortkey(0, order));              sorter.setsortkeys(keys);              switch (order) {                 case ascending:                     order = sortorder.descending;                     break;                 case descending:                     order = sortorder.unsorted;                     break;                 case unsorted:                     order = sortorder.ascending;                     break;             }          }      }      public class rowdatatablemodel extends abstracttablemodel {          private list<rowdata> data = new arraylist<>(25);          public rowdatatablemodel() {             (int index = 0; index < 10; index++) {                 data.add(new rowdata(index));             }         }          public string getkeycolumnvalue(int row) {             return data.get(row).get(0);         }          @override         public int getrowcount() {             return data.size();         }          @override         public int getcolumncount() {             return 5;         }          @override         public object getvalueat(int rowindex, int columnindex) {             return data.get(rowindex).get(columnindex);         }      }      public class rowdata {          private list<string> values = new arraylist<>();          public rowdata(int offset) {             offset *= 5;             (int index = 1; index < 5; index++) {                 values.add(character.tostring((char) ('a' + (offset + index))));             }              values.add(0, character.tostring((char) ('a' + math.random() * 26)));         }          public string get(int col) {             return values.get(col);         }      }  } 

note: however, visually, confusing user, i'd use sparingly.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -