Question

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

    }
Was it helpful?

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

OTHER TIPS

You can do something like this (copied almost as is from the Java doc)

    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). I have JTextField as input into RowFilter.

In JTextField I have 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:

    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.

Hope will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top