基于 jtextfield 过滤 JTable 不区分大小写 (Java)
我已经可以使用 JTextField 过滤 JTable,问题是区分大小写。例如,我在 Jtable 中得到这个名称:“Guillian Fox”,如果我在文本字段中写入“guillian Fox 或“GUILLIAN FOX”,则名称不会显示。
我知道 java 有 .toLowerCase 或 .toUpperCase 方法,但是使用该方法的问题是结果将很难看,因为两者都必须是大写或小写,而适当的将是大写的第一个字母,因为 。
JTable 的行来自数据库中的查询,因此,我想到的解决方案是不要直接在 jtable 中进行过滤,而是进行过滤结果的查询,但我认为是 鉴于我将对从文本字段插入或删除的每个字符进行查询,效率非常低。
@Override
public void changedUpdate(DocumentEvent arg0) {
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
}
@Override
public void insertUpdate(DocumentEvent arg0) {
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
}
@Override
public void removeUpdate(DocumentEvent arg0) {
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
}
I already can filter the JTable using a JTextField, the problem is that is case sensitive. For example, I got this name in the Jtable: "Guillian Fox", if I write "guillian fox or "GUILLIAN FOX" in the textField the name doesn't show.
I know that java have .toLowerCase or .toUpperCase methods, but the problem using that methods is the result is going to be unsightly, 'cause both have to be upper case or lower case, when the appropiate would be the first letter in uppercase, because are names.
The rows of the JTable are from a query in a data base. So, the solution I was thinking is do not do the filter directly in the jtable, instead of that, make a query that filter the results, but I think is very inefficient given that I will make a query for each character inserted or deleted from the textField.
@Override
public void changedUpdate(DocumentEvent arg0) {
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
}
@Override
public void insertUpdate(DocumentEvent arg0) {
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
}
@Override
public void removeUpdate(DocumentEvent arg0) {
ordenador.setRowFilter(RowFilter.regexFilter(jtxtfBuscarInv.getText(), 0));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对于忽略大小写,
但对于非 ASCII 语言,您必须检查键盘上与大 ENTER 一致/附近的四个键 (2x2),
如果您与此问题相关,那么您必须从键盘中排除这四个键并编写也有自己的 UpperCase 和 LoverCase 矩阵
for ignore CaseWhatever
but for non-ASCII launguages, you have to check four key (2x2) in line with/near Big ENTER on the keyboard,
if you rellated with this issue, then you have to exclude these four keys from keyboard and to write own matrix for UpperCase and LoverCase too
你可以做这样的事情(几乎从Java文档复制)
这应该匹配至少有一个字段的任何行,其中文本表示包含您创建匹配器的字符串。该类型可能需要根据您的型号进行调整。
You can do something like this (copied almost as is from the Java doc)
This should match any row which has at least one field where the textual representation contains the string you create the matcher with. The type might have to be adjusted to your model.
也许有人会帮助我的解决方案。我的问题:我有 JTable,JTable 中的数据包含国家字符(带有锐角和楔形)。我将 JTextField 作为 RowFilter 的输入。
在 JTextField 中我有 DocumentListener:
});
然后实现RowFilter:
当然需要排序器:
这样,粘贴在搜索框中的文本是上还是下并不重要,JTable中的数据是下还是上也没关系+它会翻译为上根据定义的区域设置降低。
希望能有所帮助。
Maybe to someone will help my solution. My problem: I have JTable, data in JTable contains national characters (with acutes and wedges). I have JTextField as input into RowFilter.
In JTextField I have DocumentListener:
});
Then implementation of the RowFilter:
and of course one will need sorter:
In this way it doesn't matter if text pasted in search box is upper or lower and it doesn't matter if data in JTable are lower or upper + it translate upper to lower based on defined locale.
Hope will help.