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
– user28595
@diegofm did not understand the question.
– Gustavo Santos
In this code, searching for U does not filter anything.
– user28595
It’s just an example, if you search with another letter, G for example, it will bring other results.
– Gustavo Santos
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
– user28595
@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
– Gustavo Santos
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 :)
– user28595