简体   繁体   English

基于jtextfield NON区分大小写(Java)过滤JTable

[英]Filter JTable based on a jtextfield NON Case Sensitive (Java)

I already can filter the JTable using a JTextField, the problem is that is case sensitive. 我已经可以使用JTextField过滤JTable,问题是区分大小写。 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. 例如,我在Jtable中得到了这个名字:“Guillian Fox”,如果我在textField中写“guillian fox”或“GUILLIAN FOX”这个名字没有显示。

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. 我知道java有.toLowerCase或.toUpperCase方法,但是使用这些方法的问题是结果是不雅观的,因为两者都必须是大写或小写,当适当的是大写的第一个字母时,因为是名字。

The rows of the JTable are from a query in a data base. JTable的行来自数据库中的查询。 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. 所以,我想的解决方案是不直接在jtable中进行过滤,而不是那样,做一个过滤结果的查询,但我认为是非常低效的,因为我会对插入或删除的每个字符进行查询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));

    }

for ignore CaseWhatever 忽略CaseWhatever

ordenador.setRowFilter(RowFilter.regexFilter("(?i)" + text));

but for non-ASCII launguages, you have to check four key (2x2) in line with/near Big ENTER on the keyboard, 但是对于非ASCII语言,你必须检查键盘上与Big ENTER一致的四个键(2x2),

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 如果您关注此问题,那么您必须从键盘中排除这四个键并为UpperCase和LoverCase编写自己的矩阵

You can do something like this (copied almost as is from the Java doc) 你可以做这样的事情(几乎按照Java文档复制)

    public class ContainsIgnoreCaseFilter extends RowFilter<Object, Object> {

        private final String match;

        public ContainsIgnoreCaseFilter(String match) {
            this.match = match.toLowerCase();
        }

        @Override
        public boolean include(javax.swing.RowFilter.Entry<? extends Object, ? extends Object> entry) {
            for (int i = entry.getValueCount() - 1; i >= 0; i--) {
                if (entry.getStringValue(i).toLowerCase().contains(match)) {
                  return true;
                }
              }
              return false;
        }            
    };

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. 可能必须根据您的模型调整类型。

Maybe to someone will help my solution. 也许有人会帮我解决。 My problem: I have JTable, data in JTable contains national characters (with acutes and wedges). 我的问题:我有JTable,JTable中的数据包含国家字符(有锐角和楔形)。 I have JTextField as input into RowFilter. 我有JTextField作为RowFilter的输入。

In JTextField I have DocumentListener: 在JTextField中我有DocumentListener:

textFielInput.getDocument().addDocumentListener(new DocumentListener(){
@Override
public void changedUpdate(DocumentEvent e){
    applyFilter();
}

@Override
public void insertUpdate(DocumentEvent e){
    applyFilter();
}

@Override
public void removeUpdate(DocumentEvent e){
    applyFilter();
}

}); });

Then implementation of the RowFilter: 然后执行RowFilter:

    private void applyFilter()
{
    RowFilter<MyTableModel, Integer> filter = null;
    final String filteringText = this.textFielInput.getText().toLowerCase(new Locale("cs_CZ"));
    filter = new RowFilter<MyTableModel, Integer>()
    {
        @Override
        public boolean include(Entry<? extends MyTableModel, ? extends Integer> entry)
        {
            for(int i = entry.getValueCount() - 1; i >= 0; i--)
            {
                String filteredField = entry.getStringValue(i).toLowerCase(new Locale("cs_CZ"));
                if(filteredField.contains(filteringText))
                {
                    return true;
                }
            }
            return false;
        }
    };
}
this.sorter.setRowFilter(filter);

and of course one will need sorter: 当然,人们需要分拣机:

sorter = new TableRowSorter<MyTableModel>(new MyTableModel());

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. 这样,在搜索框中粘贴的文本是上限还是下限并不重要,如果JTable中的数据低于或高于+它根据定义的区域设置从上到下翻译无关紧要。

Hope will help. 希望会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM