1

How can I implement a RowFilterbetween two dates? The dates are in string format. Is it necessary to change the format to a date format to apply a RegexFilter?

I tried using the following, but failed :

DefaultTableModel model = (DefaultTableModel) easypath.masteBusiness_table.getModel();
easypath.masteBusiness_table.setModel(model);
TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(easypath.masteBusiness_table.getModel());
easypath.masteBusiness_table.setRowSorter(rowSorter);
rowSorter.setRowFilter(RowFilter.regexFilter(startD+"\\s+(.*?)\\s+"+endD));

I know I am wrong with the filtering as (startD+"\\s+(.*?)\\s+"+endD)); is only for searching a single string but as a novice I would very much appreciate any suggestion.

UPDATE
I just saw this (http://docs.oracle.com/javase/7/docs/api/javax/swing/RowFilter.html) where RowSorter<M,I>, M is the model and I is an integer value, which does not match my criteria

mKorbel
  • 109,525
  • 20
  • 134
  • 319
mustangDC
  • 945
  • 1
  • 12
  • 33
  • Is the data stored as a Date object or as a String? – MadProgrammer Jul 12 '15 at 21:23
  • the date is stored as a String – mustangDC Jul 12 '15 at 21:24
  • 1
    It would be easier if they were stored as Date, as you can compare Dates easily, an [example](http://stackoverflow.com/questions/22495525/java-comparing-strings-which-are-numbers/22495727#22495727) of comparing String based Dates. You need to define your Owen RowFilter, which takes the start and end Dates and then compares each row to see if it's between the specified ranged. [RowFilter example](http://stackoverflow.com/questions/20958513/java-swing-toggle-button-to-filter-jtable-rows/20959348#20959348) – MadProgrammer Jul 12 '15 at 21:32
  • Will try and let you know... – mustangDC Jul 12 '15 at 21:43

2 Answers2

4

You should be storing a Date in the TableModel not a String representation of the date.

Then you can use a RowFilter.

Instead of a regex filter you can use an "and" filter with two date filters. Something like:

List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add( RowFilter.dateFilter(ComparisonType.AFTER, startDate) );
filters.add( RowFilter.dateFilter(ComparisonType.BEFORE, endDate) );
rf = RowFilter.andFilter(filters);
sorter.setRowFilter(rf);

Read the Swing tutorial on Sorting and Filtering for more information and working examples on how to use a filter.

camickr
  • 321,443
  • 19
  • 166
  • 288
1

You should have an atributte JTable table and DefaultTableModel dtm. Then in the function to filter by a range of dates.

public void filter(Date startDate, Date endDate){
    List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
    filters.add( RowFilter.dateFilter(ComparisonType.AFTER, startDate) );
    filters.add( RowFilter.dateFilter(ComparisonType.BEFORE, endDate) );
    dtm = (DefaultTableModel) table.getModel();
    TableRowSorter<DefaultTableModel> tr = new TableRowSorter<>(dtm);
    table.setRowSorter(tr);
    RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
    tr.setRowFilter(rf);}