I am making a table with a text field below it where you can type in a word to filter the table. It works, but what I want to do is be able to filter it with the word typed in, but ignoring the case of the word. Is there a way to accomplish this without creating a custom RowFilter
?
我正在制作一个带有文本字段的表格,您可以在其中输入单词来过滤表格。它有效,但我想要做的是能够用输入的单词过滤它,但忽略单词的情况。有没有办法在不创建自定义RowFilter的情况下完成此操作?
You can run this SCCEE to see what I'm talking about. I want to be able to type in usa
and it'll filter USA
.
您可以运行此SCCEE以查看我正在谈论的内容。我想能够输入美国,它将过滤美国。
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));
}
}
});
}
}
Is there maybe something I can pass to the regexFilter(text)
, that will give me the desired result?
有没有什么我可以传递给regexFilter(文本),这会给我想要的结果?
rowSorter.setRowFilter(RowFilter.regexFilter(text));
2 个解决方案
#1
26
Add the standard regex case-insensitivity flag:
添加标准的正则表达式不区分大小写标志:
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
I've tested this with your SSCCE (thanks for providing that) and it works.
我用你的SSCCE测试了这个(感谢你提供的)并且它有效。
#2
1
rowSorter.setStringConverter(new TableStringConverter() {
@Override
public String toString(TableModel model, int row, int column) {
return model.getValueAt(row, column).toString().toLowerCase();
}
});
rowSorter.setRowFilter(RowFilter.regexFilter(jtfFilter.getText().toLowerCase()));
#1
26
Add the standard regex case-insensitivity flag:
添加标准的正则表达式不区分大小写标志:
rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
I've tested this with your SSCCE (thanks for providing that) and it works.
我用你的SSCCE测试了这个(感谢你提供的)并且它有效。
#2
1
rowSorter.setStringConverter(new TableStringConverter() {
@Override
public String toString(TableModel model, int row, int column) {
return model.getValueAt(row, column).toString().toLowerCase();
}
});
rowSorter.setRowFilter(RowFilter.regexFilter(jtfFilter.getText().toLowerCase()));