javaswingjtabletablerowsorter

filtering a table model


I have 2 checkboxes in an application. The checkboxes are independent of one another ie one can be selected or both can be selected or neither can be selected.

The checkboxes filter a table model, table is refreshed every 10 seconds.

Before each refresh i get the status of each check box as follows:

checkM = checkboxM.isSelected();
checkI = checkboxI.isSelected();

After the refresh has occurred i then try and set the checkboxes to what they were as follows:

checkboxM.setSelected(checkM);
checkboxI.setSelected(checkI);

This is then followed by my if statement to find out which boxes are selected and this is where the problem lies as i cant seem to work out how to apply the filters a) that both checkboxes are selected therefore displaying all rows with content "A" and "B" b) one of the checkboxes is selected s0 that only "A" or "B" is shown or no rows are shown

if ((checkM = false) || (checkI = false)){
newFilter("",4);   
}

else{
 newFilter("A",4);
newFilter("B",4);


}



//this is my filter

private void newFilter(String x, Integer n) {
sorterDG = new TableRowSorter (modelImbalanceTable);
sorterDG.setRowFilter(RowFilter.regexFilter(x,n));
tableDG.setRowSorter(sorterDG);
tableDG.tableChanged(new TableModelEvent(tableDG.getModel())) ;
tableDG.repaint();
}

Solution

  • I don't really follow your logic. But if your logic is correct, your problem is just a simply typo. You used = where you probably wanted to use ==. = assigns new values to checkM and checkI and at the same time evaluates to the set value. == would compare the value. Besides that, comparing a boolean to get a boolean is not really helpful. Just use the boolean as is.

    However, this means you currently always execute your else path and never your if path. And inside your if path you have your next problem. You do not accumulate filters, but you first set filter A, then B. So at runtime you will always end up filtering for B, no matter what you have checked.

    You probably meant something like

    if (checkM && checkI) {
        newFilter("A|B", 4);
    } else if (checkM) {
        newFilter("A", 4);
    } else if (checkI) {
        newFilter("B", 4);
    } else {
        newFilter("", 4);
    }