我还是Java新手;我创建了一个搜索或过滤文本字段以在Jtable中查找我的数据库,但此代码仅搜索小写。如何进行可以过滤大写和小写的编码?
这是我的代码:
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
DefaultTableModel table = (DefaultTableModel)JTable_MosqueManagementSystem.getModel();
String search = jTextField1.getText().toLowerCase();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(table);
JTable_MosqueManagementSystem.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(search)); }
答案 0 :(得分:0)
通过从字符串中删除 toLowerCase()方法来尝试给定的模式
RowFilter<TableModel, Object> filter = RowFilter.regexFilter(Pattern.compile(lookup,Pattern.CASE_INSENSITIVE).toString(),0,1);
如需进一步修改,请按照给定的链接
https://coderanch.com/t/498043/java/JTable-rowFilter-case-insensitive
答案 1 :(得分:0)
您可以通过此
更改最后一行private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
DefaultTableModel table = (DefaultTableModel)JTable_MosqueManagementSystem.getModel();
String search = jTextField1.getText();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(table);
JTable_MosqueManagementSystem.setRowSorter(tr);
sorter.setRowFilter(RowFilter.regexFilter("(?i)" + "Alt",search));
}
或
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
DefaultTableModel table = (DefaultTableModel)JTable_MosqueManagementSystem.getModel();
String search = jTextField1.getText();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(table);
JTable_MosqueManagementSystem.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter("alt",Pattern.CASE_INSENSITIVE,search));
}
或试试这个
private void jTextField1KeyReleased(java.awt.event.KeyEvent evt) {
DefaultTableModel table = (DefaultTableModel)JTable_MosqueManagementSystem.getModel();
String search = jTextField1.getText();
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(table);
JTable_MosqueManagementSystem.setRowSorter(tr);
Pattern pattern = Pattern.compile("alt", Pattern.CASE_INSENSITIVE);
tr.setRowFilter(RowFilter.regexFilter(pattern,search));
}