please I have an issue with RowFilter (its ComparisonType) to put together
- Regex(String), Number, Date
together with
- AND, OR, NOR
no idea how
- to handle with null value for AND, OR, NOR
- if Regex(String), Number, Date has the same logics or there "small differencies"
- especially how to compare Date value
some longer and detailed descriptions are welcome
in this SSCCE to see my basic issues into
frame.add(new JButton(new AbstractAction("Toggle filter") {
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 with 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 with Integer
// ---> doesn't works
//return ((Number) entry.getValue(1)).intValue() > 10 |
//((Number) entry.getValue(1)).intValue() < 50;//OR with Integer
// ---> works, but initialized on 2nd. event
//return ((Number) entry.getValue(1)).intValue() > 10 !=
//((Number) entry.getValue(1)).intValue() < 50;//NOR with 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();
}
});
}
}