java - JTable, RowFilter and RowFilter.Entry -


  1. please have issue rowfilter (its comparisontype) put

    • regex(string), number, date

    together

    • and, or, nor
  2. no idea how

    • to handle null value and, or, nor
    • if regex(string), number, date has same logics or there "small differencies"
    • especially how compare date value
  3. some longer , detailed descriptions welcome

  4. in sscce see basic issues frame.add(new jbutton(new abstractaction("toggle filter") {

enter image description hereenter image description here

import java.awt.borderlayout; import java.awt.event.actionevent; import java.util.calendar; import java.util.date; import javax.swing.*; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablemodel; import javax.swing.table.tablerowsorter;  public class jtablefilterdemo {      private static tablerowsorter<tablemodel> sorter;     private object[][] data = {{"a", 5, true, new date()},         {"b", 2, false, new date()}, {"c", 4, false, new date()},         {"d", 8, true, new date()}, {"e", 13, false, new date()},         {"f", 7, true, new date()}, {"g", 55, false, new date()},         {"h", 6, false, new date()}, {"i", 1, true, new date()}};     private string columnnames[] = {"item", "value", "boolean", "date"};     private tablemodel model = new defaulttablemodel(data, columnnames) {         private static final long serialversionuid = 1l;          @override         public class<?> getcolumnclass(int column) {             switch (column) {                 case 1:                     return integer.class;                 case 2:                     return boolean.class;                 case 3:                     return date.class;                 default:                     return string.class;             }         }     };     private jtable table = new jtable(model);     private date modifdate = new date();      public jtablefilterdemo() {         modifydateintable();         table.setpreferredscrollableviewportsize(table.getpreferredsize());         rowfilter<object, number> filter = new rowfilter<object, number>() {             @override             public boolean include(rowfilter.entry entry) {                 //string str = (string) entry.getvalue(0);//string                 //return str.matches(("(?i)^a|^g"));//string                 //boolean bol = (boolean) entry.getvalue(2);//boolean                 //return bol.booleanvalue() == false;//boolean                 //date date = (date) entry.getvalue(3);//date                 //return date.gettime() > (long) (new date().gettime());//date                 //return ((number) entry.getvalue(1)).intvalue() % 2 == 0;//integer                 //return ((number) entry.getvalue(1)).intvalue() > 0;//integer                 return ((number) entry.getvalue(1)).intvalue() > 10                         & ((number) entry.getvalue(1)).intvalue() < 50;//and integer             }         };         sorter = new tablerowsorter<tablemodel>(model);         sorter.setrowfilter(filter);         table.setrowsorter(sorter);         jscrollpane scrollpane = new jscrollpane(table);         jframe frame = new jframe("filtering table");         frame.add(new jbutton(new abstractaction("toggle filter") {             private static final long serialversionuid = 1l;             private rowfilter<tablemodel, object> filter = new rowfilter<tablemodel, object>() {                 @override                 public boolean include(javax.swing.rowfilter.entry<? extends tablemodel, ? extends object> entry) {                     //string str = (string) entry.getvalue(0);//string                     //return str.matches(("(?i)^a|^g"));//string                     //boolean bol = (boolean) entry.getvalue(2);//boolean                     //return bol.booleanvalue() == false;//boolean                     //date date = (date) entry.getvalue(3);//date                     //return date.gettime() > (long) (new date().gettime());//date                     //return ((number) entry.getvalue(1)).intvalue() % 2 == 0;//integer                     //return ((number) entry.getvalue(1)).intvalue() > 0;//integer                     return ((number) entry.getvalue(1)).intvalue() > 10                             & ((number) entry.getvalue(1)).intvalue() < 50;//and integer                      // --->  doesn't works                     //return ((number) entry.getvalue(1)).intvalue() > 10 |                     //((number) entry.getvalue(1)).intvalue() < 50;//or integer                      // ---> works, initialized on 2nd. event                     //return ((number) entry.getvalue(1)).intvalue() > 10 !=                      //((number) entry.getvalue(1)).intvalue() < 50;//nor integer                  }             };              @override             public void actionperformed(actionevent e) {                 if (sorter.getrowfilter() != null) {                     sorter.setrowfilter(null);                 } else {                     sorter.setrowfilter(filter);                 }             }         }), borderlayout.south);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.add(scrollpane);         frame.pack();         frame.setvisible(true);     }      private void modifydateintable() {         calendar c = calendar.getinstance();         c.settime(modifdate);         c.add(calendar.date, - 1);         modifdate = c.gettime();         table.setvalueat(modifdate, 0, 3);         c.settime(modifdate);         c.add(calendar.date, +5);         modifdate = c.gettime();         table.setvalueat(modifdate, 1, 3);         c.settime(modifdate);         c.add(calendar.date, +1);         modifdate = c.gettime();         table.setvalueat(modifdate, 2, 3);         c.settime(modifdate);         c.add(calendar.date, - 16);         modifdate = c.gettime();         table.setvalueat(modifdate, 3, 3);         c.settime(modifdate);         c.add(calendar.date, +30);         modifdate = c.gettime();         table.setvalueat(modifdate, 4, 3);         c.settime(modifdate);         c.add(calendar.date, +55);         modifdate = c.gettime();         table.setvalueat(modifdate, 5, 3);         c.settime(modifdate);         c.add(calendar.date, +155);         modifdate = c.gettime();         table.setvalueat(modifdate, 6, 3);         c.settime(modifdate);         c.add(calendar.date, -23);         modifdate = c.gettime();         table.setvalueat(modifdate, 7, 3);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 jtablefilterdemo jtfd = new jtablefilterdemo();             }         });     } } 

i see no unexpected result.

  1. the first predicate straightforward; includes intersection of 2 overlapping sets, row having value 13 member.

    return ((number) entry.getvalue(1)).intvalue() > 10     & ((number) entry.getvalue(1)).intvalue() < 50; 
  2. the second predicate union of 2 overlapping sets, rows included.

    return ((number) entry.getvalue(1)).intvalue() > 10 |     ((number) entry.getvalue(1)).intvalue() < 50; 
  3. the third predicate complement of first, row having value 13 alternately included , excluded.

    return ((number) entry.getvalue(1)).intvalue() > 10 !=      ((number) entry.getvalue(1)).intvalue() < 50; 

addendum: regarding rowfilter.entry, found helpful examine how and, or , not factories use index parameter defined in private class rowfilter.generalfilter; each subclass invokes parent's implementation of include() each filter supplied in constructor. in contrast, date, number , regex factories not; instead, each relies on predicates available underlying type.

addendum: concrete example, select 2 model dates, e.g. first , penultimate.

date d1 = (date) model.getvalueat(0, 3); date d2 = (date) model.getvalueat(model.getrowcount() - 2, 3); 

create 2 filters bracket chosen dates.

rowfilter<tablemodel, integer> low =      rowfilter.datefilter(rowfilter.comparisontype.after, d1, 3); rowfilter<tablemodel, integer> high =      rowfilter.datefilter(rowfilter.comparisontype.before, d2, 3); 

combine them in andfilter.

list<rowfilter<tablemodel, integer>> filters = arrays.aslist(low, high); final rowfilter<tablemodel, integer> filter = rowfilter.andfilter(filters); 

the result selects rows between, excluding, d1 , d2.

modified sscce:

import java.awt.borderlayout; import java.awt.event.actionevent; import java.util.arrays; import java.util.calendar; import java.util.date; import java.util.list; import javax.swing.*; import javax.swing.table.defaulttablemodel; import javax.swing.table.tablemodel; import javax.swing.table.tablerowsorter;  public class jtablefilterdemo {      private static tablerowsorter<tablemodel> sorter;     private object[][] data = {{"a", 5, true, new date()},         {"b", 2, false, new date()}, {"c", 4, false, new date()},         {"d", 8, true, new date()}, {"e", 13, false, new date()},         {"f", 7, true, new date()}, {"g", 55, false, new date()},         {"h", 6, false, new date()}, {"i", 1, true, new date()}};     private string columnnames[] = {"item", "value", "boolean", "date"};     private tablemodel model = new defaulttablemodel(data, columnnames) {         @override         public class<?> getcolumnclass(int column) {             switch (column) {                 case 1:                     return integer.class;                 case 2:                     return boolean.class;                 case 3:                     return date.class;                 default:                     return string.class;             }         }     };     private jtable table = new jtable(model);      public jtablefilterdemo() {         modifydateintable();         table.setpreferredscrollableviewportsize(table.getpreferredsize());         sorter = new tablerowsorter<tablemodel>(model);         table.setrowsorter(sorter);         date d1 = (date) model.getvalueat(0, 3);         date d2 = (date) model.getvalueat(model.getrowcount() - 2, 3);         rowfilter<tablemodel, integer> low = rowfilter.datefilter(rowfilter.comparisontype.after, d1, 3);         rowfilter<tablemodel, integer> high = rowfilter.datefilter(rowfilter.comparisontype.before, d2, 3);         list<rowfilter<tablemodel, integer>> filters = arrays.aslist(low, high);         final rowfilter<tablemodel, integer> filter = rowfilter.andfilter(filters);         jscrollpane scrollpane = new jscrollpane(table);         jframe frame = new jframe("filtering table");         frame.add(new jbutton(new abstractaction("toggle filter") {             @override             public void actionperformed(actionevent e) {                 if (sorter.getrowfilter() != null) {                     sorter.setrowfilter(null);                 } else {                     sorter.setrowfilter(filter);                 }             }         }), borderlayout.south);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.add(scrollpane);         frame.pack();         frame.setlocationbyplatform(true);         frame.setvisible(true);     }      private void modifydateintable() {         date modifdate = new date();         calendar c = calendar.getinstance();         c.settime(modifdate);         c.add(calendar.date, - 1);         modifdate = c.gettime();         table.setvalueat(modifdate, 0, 3);         c.settime(modifdate);         c.add(calendar.date, +5);         modifdate = c.gettime();         table.setvalueat(modifdate, 1, 3);         c.settime(modifdate);         c.add(calendar.date, +1);         modifdate = c.gettime();         table.setvalueat(modifdate, 2, 3);         c.settime(modifdate);         c.add(calendar.date, - 16);         modifdate = c.gettime();         table.setvalueat(modifdate, 3, 3);         c.settime(modifdate);         c.add(calendar.date, +30);         modifdate = c.gettime();         table.setvalueat(modifdate, 4, 3);         c.settime(modifdate);         c.add(calendar.date, +55);         modifdate = c.gettime();         table.setvalueat(modifdate, 5, 3);         c.settime(modifdate);         c.add(calendar.date, +155);         modifdate = c.gettime();         table.setvalueat(modifdate, 6, 3);         c.settime(modifdate);         c.add(calendar.date, -23);         modifdate = c.gettime();         table.setvalueat(modifdate, 7, 3);     }      public static void main(string[] args) {         swingutilities.invokelater(new runnable() {             @override             public void run() {                 jtablefilterdemo jtfd = new jtablefilterdemo();             }         });     } } 

Comments

Popular posts from this blog

curl - PHP fsockopen help required -

HTTP/1.0 407 Proxy Authentication Required PHP -

c# - Resource not found error -