如何在 JTable 上执行不区分大小写的筛选器?
我正在制作一个表格,下面有一个文本字段,您可以在其中键入单词来过滤表格。它有效,但我想做的是能够用输入的单词来过滤它,但忽略单词的大小写。有没有办法在不创建自定义的情况下实现这一点?RowFilter
您可以运行此 SCCEE 来查看我在说什么。我希望能够输入,它将过滤。usa
USA
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
public class TestTableSortFilter extends JApplet {
private String[] columnNames
= {"Country", "Capital", "Population in Millions", "Democracy"};
private Object[][] data = {
{"USA", "Washington DC", 280, true},
{"Canada", "Ottawa", 32, true},
{"United Kingdom", "London", 60, true},
{"Germany", "Berlin", 83, true},
{"France", "Paris", 60, true},
{"Norway", "Oslo", 4.5, true},
{"India", "New Delhi", 1046, true}
};
private JTable jTable = new JTable(data, columnNames);
private TableRowSorter<TableModel> rowSorter
= new TableRowSorter<>(jTable.getModel());
private JTextField jtfFilter = new JTextField();
private JButton jbtFilter = new JButton("Filter");
public TestTableSortFilter() {
jTable.setRowSorter(rowSorter);
JPanel panel = new JPanel(new BorderLayout());
panel.add(new JLabel("Specify a word to match:"),
BorderLayout.WEST);
panel.add(jtfFilter, BorderLayout.CENTER);
add(panel, BorderLayout.SOUTH);
add(new JScrollPane(jTable), BorderLayout.CENTER);
jtfFilter.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
String text = jtfFilter.getText();
if (text.trim().length() == 0) {
rowSorter.setRowFilter(null);
} else {
rowSorter.setRowFilter(RowFilter.regexFilter(text));
}
}
});
}
}
有没有一些东西我可以传递给,这将给我想要的结果?regexFilter(text)
rowSorter.setRowFilter(RowFilter.regexFilter(text));