Problems with RowFilter result

3

I'm working with DocumentListener and RowFilter , and I noticed that my filter is picking up the data I type, and searching by matching all columns in my table. I would like to know, how could I choose the column that it should compare, and also, how should I make it so that it takes only the words that start equal to what was typed.

Example query:

Ukraine → starts with U

Brazil

Portugal → It has the letter U, however, it does not start with the letter U, it should ignore it

USA → begins with U

Type U, it would only show me USA and Ukraine.

Code sample:

package teste;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class TestX extends JFrame {

    private String[] colunas
            = {"Country", "Capital", "Population in Millions", "Democracy"};

    private Object[][] dados = {
        {"USA", "Washington DC", 280, true},
        {"Canada", "Ottawa", 32, true},
        {"United Kingdom", "London", 60, true},
        {"Germany", "Berlin", 83, true},
        {"France", "Paris", 60, true},
        {"Norway", "Oslo", 4.5, true},
        {"India", "New Delhi", 1046, true}
    };

    private DefaultTableModel model = new DefaultTableModel(dados, colunas);
    private JTable tabela = new JTable(model);

    private TableRowSorter<TableModel> rowSorter = new TableRowSorter<>(tabela.getModel());

    private JTextField barraPesquisa = new JTextField();
    private JButton botao = new JButton("OK");

    private JPanel painel = new JPanel();
    private JLabel label = new JLabel();

    public TestX() {
        add(x());
        setSize(600, 400);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public JComponent x() {
        JPanel painelX = new JPanel();
        painelX.setLayout(null);

        painelX.add(barraPesquisa);
        barraPesquisa.setBounds(20, 10, 270, 27);
        painelX.add(botao);
        botao.setBounds(300, 10, 65, 27);

        barraPesquisa.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                String text = barraPesquisa.getText();

                //model.setRowCount(0); // limpar as linhas
                if (text.trim().length() == 0) {
                    barraPesquisa.setText("Digite aqui..");
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                    model.getRowCount();
                }
                label.setText(rowSorter.getViewRowCount() + " resultados encontrados");
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                String text = barraPesquisa.getText();
                if (text.trim().length() == 0) {
                    rowSorter.setRowFilter(null);
                } else {
                    rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
                }
                label.setText(rowSorter.getViewRowCount() + " resultados encontrados");
            }

            @Override
            public void changedUpdate(DocumentEvent e) {

            }
        });

        tabela.setRowSorter(rowSorter);

        painelX.add(tabela);
        tabela.setBounds(5, 100, 500, 150);

        painelX.add(label);
        label.setBounds(10, 250, 150, 30);
        label.setText(model.getRowCount() + " resultados encontrados!");

        painelX.setBounds(1, 1, 600, 400);
        return painelX;
    }

    public static void main(String[] args) {
        TestX t = new TestX();
    }
}
    
asked by anonymous 15.03.2017 / 13:58

1 answer

2

If it is only to limit the filter to search from the initial words of the table, simply change your filter to:

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

The ^ will cause the filter to be applied from the beginning of the line, not in any position that the searched term is found.

Other Patterns can be viewed on this page

You can also create different filters for each column or limit to just a few, using some methods of class RowFilter . From the code presented in the question, to restrict the search to only the first column, simply enter the index of the column in the own RowFilter.regexFilter() :

// o indice 0 filtrará apenas a primeira coluna
rowSorter.setRowFilter(RowFilter.regexFilter("^(?i)" + text, 0));
    
15.03.2017 / 14:11