1

I am trying to filter my table by value in text field but whenever I write anything to my text field all rows in table all filtered (so there are no rows displayed) even if the table contains that word or number.

public JTextField filterT = new JTextField();
private TableRowSorter<TableModel> sorter;

        sorter = new TableRowSorter<TableModel>(tm); //tm is my table model
        filterT.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = filterT.getText();
                filter(text);
            }
            @Override
            public void removeUpdate(DocumentEvent e) 
            { 
                String text = filterT.getText();
                filter(text);
            }
            @Override
            public void changedUpdate(DocumentEvent e) {
                String text = filterT.getText();
                filter(text);
            }
          });

private void filter(String text)
{
    RowFilter<TableModel, Object> rf = null;
    try {
        rf = RowFilter.regexFilter(text, 0);
    } catch (java.util.regex.PatternSyntaxException ee) {
        return;
    }
    sorter.setRowFilter(rf);
    jTable1.setRowSorter(sorter);
}

What is wrong ?

Dakado
  • 143
  • 1
  • 2
  • 9

2 Answers2

0

Read the section from the Swing tutorial on Sorting and Filtering for a working example that does this.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • Lol did you read my code ? It is based on that example but is does not work! – Dakado Jul 18 '14 at 20:13
  • So why does the tutorial example work??? What is different from the working code and your code??? Start with the working code and make changes one at a time. Then when it stops working you know what you have changes and then can ask a specific question about the problem. We don't have access to your data. We don't know what you are typing into the text field. Don't expect us to do your debugging for you!!! – camickr Jul 18 '14 at 20:31
0

I think you can try this

DefaultTableModel Model = (DefaultTableModel)jTableName.getModel(); 
TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(Model);
jTableName.setRowSorter(tr);
tr.setRowFilter(RowFilter.regexFilter(JtextfieldName.getText().trim()));
Alexis
  • 1,685
  • 1
  • 12
  • 30