Search code examples
javaswingjtablecase-sensitiverowfilter

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. 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));

    }

Solution

  • for ignore 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,

    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