javaregexswingjtablerowfilter

Java JTable RowFilter doesn't work properly?


I have a JTable (DefaultTableModel) containing various rows of data I'd like to filter. The program correctly figures out which rows to filter and adds the regex string (corresponding to the data of a specific column as no string matches all) to an ArrayList of RowFilters. This ArrayList is then set as a RowFilter.andFilter. But where there should be data to show, there is usually nothing or some kind of broken data. I tried various approaches the last 2 days and read countless things online, but I can't get the filter to work properly. Where is the mistake?

In the function that should trigger the filter, it looks like this:

TableRowSorter<TableModel> sorter = new TableRowSorter<>(table.getModel());
sorter.setRowFilter(null);
table.setRowSorter(sorter);

List<RowFilter<Object, Object>> filterList = new ArrayList<RowFilter<Object, Object>>();

boolean missing = false;

//some logic to set missing to true/false depending on whether to filter or not

if (missing)
    filterList.add(RowFilter.regexFilter(dataString)); //dataString is the exact string in a specific column of the row to be filtered

And after the calculations are complete, the filter should be executed:

sorter.setRowFilter(RowFilter.andFilter(filterList, 5));

Now the table is just going blank. No data whatsoever. The boolean missing is always correct (debugged it)...

I tried to extend the regex string with ^ in the front and $ at the end or . ... . or putting it in brackets "(" + dataString + ")" but it's no use. I even tried it without the column index. The outcome is always the same while at least 6 rows should remain.

Appreciate your help.


This is a MCVE. The program is correctly calculating the values that shall not be shown in the table but the result is a blank table...

import java.awt.FlowLayout;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JPanel;
import javax.swing.RowFilter;
import javax.swing.table.*;

public class mainTable extends javax.swing.JFrame {

private TableRowSorter<TableModel> sorter;

public mainTable() {
    initComponents();
    
    sorter = new TableRowSorter<>(table.getModel());
    sorter.setRowFilter(null);
    table.setRowSorter(sorter);
    
    setTable();
    setCombo();
}

private void initComponents() {
    jPanel = new JPanel();
    table = new javax.swing.JTable();
    comboBox = new javax.swing.JComboBox<>();
    jButton1 = new javax.swing.JButton();
    
    setLayout(new FlowLayout());
    setSize(400, 400);
    
    DefaultTableModel tableModel = new DefaultTableModel();
    tableModel.addColumn("Stadt");
    tableModel.addColumn("Landkreis");
    table.setModel(tableModel);

    jButton1.setText("Filter");
    jButton1.addActionListener(this::jButton1ActionPerformed);
    
    jPanel.add(comboBox);
    jPanel.add(jButton1);
    jPanel.add(table);
    getContentPane().add(jPanel);
}                   

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    filter();
}                                        

public static void main(String args[]) {
    new mainTable().setVisible(true);
}

private void setTable() {
    ArrayList<String> cities = new ArrayList<>();
    ArrayList<String> landkreise = new ArrayList<>();
    String[] rows = new String[2];
    
    DefaultTableModel tbm = (DefaultTableModel) table.getModel();
    
    cities.add("Husum");
    cities.add("Westerland");
    cities.add("Hannover");
    cities.add("Uelzen");
    cities.add("Rotenburg");
    cities.add("Verden");
    cities.add("Nienburg");
    
    landkreise.add("Nordfriesland");
    landkreise.add("Nordfriesland");
    landkreise.add("Region Hannover");
    landkreise.add("Uelzen");
    landkreise.add("Rotenburg");
    landkreise.add("Verden");
    landkreise.add("Nienburg");
    
    for (int i = 0; i < cities.size(); i++) { 
        rows[0] = cities.get(i);
        rows[1] = landkreise.get(i);
        tbm.addRow(rows);
    }
}

private void setCombo() {        
    DefaultTableModel tbm = (DefaultTableModel) table.getModel();
    
    for (int i = 0; i < tbm.getRowCount(); i++)
        comboBox.addItem(tbm.getValueAt(i, 1).toString());
}
     
private void filter() {      
    sorter.setRowFilter(null);
     
    String filterText = comboBox.getSelectedItem().toString();
    List<RowFilter<Object, Object>> filterLandkreis = new ArrayList<RowFilter<Object, Object>>();
                 
    for (int i = table.getRowCount(); i > 0; i--) {   
        
        boolean filter = false;
        filter = !table.getValueAt(i-1, 1).toString().equals(filterText); //the value of the combo shall not be shown in the table
        
        if (filter)
            filterLandkreis.add(RowFilter.regexFilter(table.getValueAt(i-1, 1).toString()));
    }            
    sorter.setRowFilter(RowFilter.andFilter(filterLandkreis));
}
               
private javax.swing.JComboBox<String> comboBox;
private javax.swing.JButton jButton1;
private JPanel jPanel;
private javax.swing.JTable table;                 

}


Solution

  • The thing is: I misunderstood the logic behind the RowFilter.andFilter(). I changed it to RowFilter.orFilter() and it works properly.

    If there are multiple RowFilter and you want them to filter independently, it's a logical OR, not an AND...