我在每个列标题上方都有一个jtextfield
我试图按照>这样的数字进行过滤麻木,< num,!num,== num,< = num,> = num。
例如,如果我通过拆分在jtextfield
中输入> = 100,则应该获取大于或等于100的过滤结果。
我在按钮动作侦听器方法中使用了很多if
else
条件,但是在其他情况下很少有其他条件不能正常工作,但是当单独运行时它正在工作。如何解决这个问题?
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();
}
});
}
}
答案 0 :(得分:1)
以下代码肯定会解决您的问题。
b1.addActionListener(new ActionListener() {
private List<String> operaters=Arrays.asList(new String []{">", "<", "!", "==", "<=", ">="});
private String filterPattern="[0-9]|[A-Z]|[a-z]|[0-9]";
public void actionPerformed(ActionEvent e) {
applyFilter(jtf1.getText(),0);
}
private void applyFilter(String text,int columnIndex) {
if (text.length() == 0) {
//sorter.setRowFilter(null);
}else{
String operator=text.toString().split(filterPattern)[0];
if(operaters.contains(operator)){
String value=text.split(operator).length>1?text.split(operator)[1]:"";
sorter.setRowFilter(getRowFilter(operator,value,columnIndex));
}
}
}
private RowFilter<? super TableModel, ? super Integer> getRowFilter(
String operator, String value,int columnIndex) {
if (operator.equals("==")) {
return RowFilter.regexFilter(value);
} else if (operator.equals("!")) {
return RowFilter.notFilter(RowFilter.regexFilter(value,columnIndex));
}
else if (operator.equals(">")) {
return RowFilter.numberFilter(ComparisonType.AFTER,
Integer.parseInt(value),columnIndex);
} else if (operator.equals("<")) {
Number numb = Integer.parseInt(value);
return RowFilter.numberFilter(ComparisonType.BEFORE, numb,columnIndex);
} else if (operator.equals(">=")) {
Number numb = Integer.parseInt(value);
// String s=text.substring(2);
List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(
2);
filters.add(0,
RowFilter.numberFilter(ComparisonType.EQUAL, numb,columnIndex));
filters.add(0,
RowFilter.numberFilter(ComparisonType.AFTER, numb,columnIndex));
return RowFilter.orFilter(filters);
}
else if (operator.equals("<=")) {
Number numb = Integer.parseInt(value);
// String s=text.substring(2);
List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(
2);
filters.add(0,
RowFilter.numberFilter(ComparisonType.EQUAL, numb,columnIndex));
filters.add(0,
RowFilter.numberFilter(ComparisonType.BEFORE, numb,columnIndex));
return RowFilter.orFilter(filters);
}
return null;
}
}
);