Problems with Rowfilter output

Asked

Viewed 165 times

3

I’m working with Documentlistener and Rowfilter, and I realized that my filter is taking the data that I type, and searching for correspondence in all the columns of my table. I would like to know, how could I choose the column that he should compare, and also, how should I make it so that he takes only the words that the beginning equals what was typed.

Example query:

Ukraine begins with U

Brazil

Portugal Has the letter U, however, does not start with the letter U, should ignore - la

USA starts with U

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

Code example:

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();
    }
}
  • Your search does not return anything with just the letter U

  • @diegofm did not understand the question.

  • In this code, searching for U does not filter anything.

  • It’s just an example, if you search with another letter, G for example, it will bring other results.

  • Gustavo take a look at the link, you deleted the question but I was testing your code and I ended up finding a way independent of those links I suggested: https://chat.stackexchange.com/rooms/63959/criando-checkboxlist-swing

  • @Articuno I had erased because as I already had there the answer I thought I would not need the question more, if you want I do it again so you put your answer

  • Test the link chat code there, but I think the answers I indicated are more complete. I just wanted to leave one more option besides the links :)

Show 2 more comments

1 answer

2


If it’s just to limit the filter to search from the initial words of the table, just change your filter to:

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

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

Other Patterns can be consulted on this page


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

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

    It continues to pick off the first letter. If you search "N", it brings Norway and India, the letter n is only the second in the word India.

  • @Gustavosantos he brings the line of India because of the city of "New Delhi".

  • 1

    So in Rowfilter there is some kind of "getColumn"? something to choose the column that should be searched the occurrence of the given/letter that was typed ?

  • @Gustavosantos do you want to restrict in which column to search? If yes, it is perfectly possible, just say which column you want to restrict to search I edit the answer.

  • 1

    the second column, so the first I always leave code/ID.

  • @Gustavosantos the column of cities?

  • 1

    sorry, forgot that the order in the example ta different, can do with the first column. Thank you !

  • @Gustavosantos edited

  • 1

    I didn’t think it would be so simple, thank you !

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.