1
I’m using RowFilter
to create filters for certain columns in my table. My intention is to pass the components that I want to use as "bar" search by Paramento, creating them dynamically, and likewise apply filters to them, which correspond to a column of the table, and finally, to be able to combine these filters if necessary.
I created an example, where I have 4 columns, I want to be able to filter the first 3 columns, being able to combine the "fields" of filtering.
I took a look at that question, Apply filters to a Jtable, which is very similar to what I’m trying to do, but in this case it passes fixed filters.
The error is occurring when I try to capture the filter texts, it throws me this exception:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at pesquisa.UmTeste.setaFiltrosPesquisa(UmTeste.java:96)
at pesquisa.UmTeste.montaTela(UmTeste.java:64)
Note 1: in the example I am only dealing with components of the type JTextComponent
, I know that I will have to make changes to cover the other components, in addition to having to treat columns that cannot/should be treated with strings
.
Note 2: I tried to make it as simple as possible, just to illustrate the problem, in this case the components are somewhat fixed, in the matter of quantity.
My example:
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.text.JTextComponent;
public class UmTeste extends JFrame {
private String[] colunas = {"0", "1", "2", "3"};
private Object[][] dados = {
{"1", "Estados Unidos", "USA", "true"},
{"2", "Canada", "CNA", "true"},
{"3", "United Kingdom", "UN", "true"},
{"4", "Germany", "GER", "true"},
{"5", "France", "FRA", "true"}};
private DefaultTableModel dtm = new DefaultTableModel(dados, colunas);
private JTable tabela = new JTable(dtm);
private TableRowSorter<TableModel> sorter;
private JScrollPane jsp = new JScrollPane(tabela);
private JComponent[] componentes = {new JTextField(), new JTextField(), new JTextField()};
public UmTeste() {
setTitle("Teste - RowFilter");
add(montaTela());
pack();
setLocationRelativeTo(null);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public JComponent montaTela() {
JPanel painel = new JPanel();
painel.setLayout(new BorderLayout());
//resgata o TableModel da sua JTable
TableModel model = tabela.getModel();
//Cria um RowSorter baseado no TableModel resgatado
sorter = new TableRowSorter<>(model);
//Aplica o RowSorte na na JTable
tabela.setRowSorter(sorter);
painel.add(pesquisa(), BorderLayout.NORTH);
painel.add(jsp, BorderLayout.CENTER);
setaFiltrosPesquisa();//seta o filtro
return painel;
}
private JComponent pesquisa() {
JPanel painelPesquisa = new JPanel();
painelPesquisa.setLayout(new FlowLayout());
for (int i = 0; i < componentes.length; i++) {
if (componentes[i] != null) {
if (componentes[i] instanceof JTextComponent) {
painelPesquisa.add(new JLabel("Coluna " + i));
painelPesquisa.add(componentes[i]);
componentes[i].setPreferredSize(new Dimension(80, 22));
}
}
}
return painelPesquisa;
}
private void setaFiltrosPesquisa() {
String[] filtros = {""};
for (int i = 0; i < componentes.length; i++) {
if (componentes[i] != null) {
if (componentes[i] instanceof JTextComponent) {
filtros[i] = ((JTextComponent) componentes[i]).getText().trim();
}
//cria uma lista para guardar os filtros de cada coluna
List<RowFilter<Object, Object>> filters = new ArrayList<>();
for (int ii = 0; ii < filters.size(); i++) {
filters.add(RowFilter.regexFilter("(?i)" + filtros[i], ii));
sorter.setRowFilter(RowFilter.andFilter(filters));
}
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
UmTeste t = new UmTeste();
});
}
}
@Gustavosantos see the edition.
– user28595
"There are other errors in your code like setting rowFilter inside the loop each time you add a filter in Arraylist, but it escapes the focus of the question." can’t explain ? if that’s the case I can edit the question.
– Gustavo Santos
@Gustavosantos his second tie is confused, not to understand his intention with him. But the line
sorter.setRowFilter(RowFilter.andFilter(filters));
has no need to stay inside a loop.– user28595
is because it returned me Exception in thread "AWT-Eventqueue-0" java.lang.Nullpointerexception, I thought I should put - it according to the filters.
– Gustavo Santos
@Gustavosantos you are creating an empty filter list, this one will apparently never run.
– user28595
Let’s go continue this discussion in chat.
– user28595