1

Question again about my Jtable ! I filter the results with JCheckBoxes ! When I check one checkbox the results are ok , but when I check two or more the results appear and follow the filter of the last JCheckBox rather than the filter of all checked JCheckBoxes . It's important to say that all checkboxes refered to one column of the table and are outside the table and used only for filtering the results in it.

My filter function is this

private void newFilter(String age){
    RowFilter<DefaultTableModel,Object> rf = null;
    try{
        rf = RowFilter.regexFilter("20");
        }catch(java.util.regex.PatternSyntaxException e){
        return;
    }
    sorter.setRowFilter(rf);
}

EDIT : In the Table there are only the results , that should appear ! The ckeckboxes are outside the Jtable and are used only for filters , for example if I check the checkbox 20 in the table will appear only people with age 20 , but if I check age 20 and age 40 in the table will appear only the people with age of 40 and also with age 20 !

mKorbel
  • 109,525
  • 20
  • 134
  • 319
gimbo
  • 67
  • 1
  • 18
  • Like [this](http://stackoverflow.com/a/4528604/230513) or maybe [this](http://stackoverflow.com/a/17856131/230513)? – trashgod Feb 06 '14 at 18:42
  • @trashgod None of them ! In the Table there are only the results , that should appear ! The ckeckboxes are outside the Jtable and are used only for filters , for example if I check the checkbox 20 in the table will appear only people with age 20 , but if I check age 20 and age 40 in the table will appear only the people with age of 40 and also with age 20 ! – gimbo Feb 06 '14 at 18:53
  • Pleas update your question to include this clarification. – trashgod Feb 06 '14 at 19:24

2 Answers2

2

The ckeckboxes are outside the JTable and are used only for filters…

You can combine and apply JTable row filters as shown here and here. Use Action to encapsulate the functionality, as shown here. Let the Action update the model, and the listening view will update itself in response.

Addendum: As a concrete example, change the JButton in the example cited to a JCheckBox.

frame.add(new JCheckBox(new AbstractAction("Toggle filter")…

image

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • I have seen Java Tutorial but I didn't finf a solution :S – gimbo Feb 06 '14 at 19:34
  • Also you provide me solutions about something else , I think ! can't come across to anything ! – gimbo Feb 06 '14 at 19:42
  • I think that I'm not clear ... I can Filter the table with one checkbox , the problem is when I try to filter it with two or more checkboxes ! I can't get your example if you can transform my given code to do what I'm saying I would be grateful ! Otherwise provide me other more easy example ! – gimbo Feb 06 '14 at 20:04
  • 1
    Instead of `RowFilter.andFilter()`, try `RowFilter.orFilter()`; more [here](http://en.wikipedia.org/wiki/Inequality_%28mathematics%29). For more detailed guidance, please edit your question to include a [*Minimal, Complete, Tested and Readable Example*](http://stackoverflow.com/help/mcve) that shows your current approach. – trashgod Feb 06 '14 at 22:53
0

Taking in concern the last comment of @trashgod I came accross and changed my filter like this and now it works perfectly :

private void newFilter() {
    RowFilter<Object, Object> rf = null;
    List<RowFilter<Object, Object>> filters = new ArrayList<RowFilter<Object, Object>>(
            2);
        if (c20.isSelected()) {
            filters.add(RowFilter.regexFilter("20"));
        }
        if (c40.isSelected()) {
            filters.add(RowFilter.regexFilter("40"));
        }

    try {
        rf = RowFilter.orFilter(filters);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
}
gimbo
  • 67
  • 1
  • 18