jtable中的java数字过滤
我在每个列标题上方都有一个jtextfield
,我试图用它来过滤像>;麻木,<;嗯!num,==num,<=num,>=num
例如,如果我输入>=通过拆分jtextfield
中的100,可以获取大于或等于100的数字的过滤结果
我在button action listener方法中使用了许多if
{
import javax.swing.RowFilter.ComparisonType;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.*;
public class TableFilter {
public TableFilter() {
Object[][] rows
= {{100, "Aaa",10},
{101, "Bbb",11},
{102, "Ccc",5},
{103, "Ddd",3},
{104, "Eee",2},
{105, "Fff",5},
{106, "Ggg",11},
{107, "Hhh",8} };
String[] column = {"empno", "name","experience"};
TableModel model = new DefaultTableModel(rows, column) {
public Class getColumnClass(int column) {
Class returnValue;
if ((column >= 0) && (column < getColumnCount())) {
returnValue = getValueAt(0, column).getClass();
} else {
returnValue = Object.class;
}
return returnValue;
}
};
final JTable table = new JTable(model);
final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
table.setRowSorter(sorter);
JTextField jtf1 = new JTextField(5);
JTextField jtf2 = new JTextField(5);
JTextField jtf3 = new JTextField(5);
JButton b1 = new JButton ("ENTER");
///////////////////EQUAL
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
String text = jtf1.getText();
if (text.length() == 0) {
sorter.setRowFilter(null);
}
else if(text.startsWith("="))
{
String[] parts = text.split("=");
String part1 = parts[1];
sorter.setRowFilter(RowFilter.regexFilter(part1));}
else if(text.startsWith("!"))
{
String[] parts = text.split("!");
String part1 = parts[1];
sorter.setRowFilter(RowFilter.notFilter(RowFilter.regexFilter(part1)));
}
else if(text.startsWith(">"))
{
String[] parts = text.split(">");
String part1 = parts[1];
sorter.setRowFilter(RowFilter.numberFilter(ComparisonType.AFTER, Integer.parseInt(part1)));
}
else if(text.startsWith("<"))
{
//System.out.println("*****************");
String[] parts = text.split("<");
String part1 = parts[1];
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
Number numb=Integer.parseInt(part1);
filters.add(0,RowFilter.notFilter(RowFilter.numberFilter(ComparisonType.AFTER,numb)));
filters.add(0,RowFilter.notFilter(RowFilter.numberFilter(ComparisonType.EQUAL,numb)));
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<>(dtm);
table.setRowSorter(tr);
RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
tr.setRowFilter(rf);
}
else if(text.startsWith(">="))
{
System.out.println("*****************");
String[] p1=text.split(">");
String p2=p1[1];
String[] p3=p2.split("=");
String pf=p3[1];
Number numb=Integer.parseInt(pf);
// String s=text.substring(2);
List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add(0,RowFilter.numberFilter(ComparisonType.EQUAL,numb));
filters.add(0,RowFilter.numberFilter(ComparisonType.AFTER,numb));
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<>(dtm);
table.setRowSorter(tr);
RowFilter<Object, Object> rf = RowFilter.orFilter(filters);
tr.setRowFilter(rf);
}
else if(text.startsWith("<="))
{
String[] p1=text.split("<");
String p2=p1[1];
String[] p3=p2.split("=");
String pf=p3[1];
//Number numb=Integer.parseInt(pf);
//String sub=text.substring(2);
sorter.setRowFilter(RowFilter.notFilter(RowFilter.numberFilter(ComparisonType.AFTER, Integer.parseInt(pf))));
}
}
}
);
JPanel panel = new JPanel(new FlowLayout());
panel.add(jtf1);
panel.add(Box.createHorizontalStrut(350));
panel.add(jtf2);
panel.add(Box.createHorizontalStrut(350));
panel.add(jtf3);
panel.add(Box.createHorizontalStrut(150));
panel.add(b1);
JFrame frame = new JFrame();
frame.add(panel, BorderLayout.BEFORE_FIRST_LINE);
frame.add(new JScrollPane(table), BorderLayout.CENTER);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new TableFilter();
}
});
}
}
# 1 楼答案
下面的代码肯定会解决您的问题