0

I have a Jtable in my gui in which I project some results ! I have also 3 JCheckBoxes that are used as filters ! The problem is that when I check a JCheckBox for first time the results are filtered correctly , but when I uncheck the JCheckBox the results remain the same as with the filter applied , which I don't want to !

JCheckBox Listener :

cEntertainment.addItemListener(new ItemListener(){
        public void itemStateChanged(ItemEvent e){
            int state = e.getStateChange();
            if (state == ItemEvent.SELECTED) {
                man.setEnabled(true);
                woman.setEnabled(true);
                child.setEnabled(true);
                newFilter(cEntertainment.getText());
            } else {
                man.setEnabled(false);
                man.setSelected(false);
                woman.setEnabled(false);
                woman.setSelected(false);
                child.setEnabled(false);
                child.setSelected(false);

            }
        }
    });

newFilter method :

private void newFilter(String type){
    RowFilter<DefaultTableModel,Object> rf = null;
    try{
        rf = RowFilter.regexFilter(type);
    }catch(java.util.regex.PatternSyntaxException e){
        return;
    }
    sorter.setRowFilter(rf);
}
gimbo
  • 67
  • 1
  • 18
  • 1) The reason your last question was not answered is because you provide to little information to go off of. Look at [**How to create a Minimal, Complete, Tested and Readable example**](http://stackoverflow.com/help/mcve). 2) Use Java naming convention. Varaibles start with lower case letters. – Paul Samsotha Feb 06 '14 at 16:27
  • Tell me what else should I upload ?! Screenshots ? I have uploaded the code in which I think the error exists ! I'm newbie here ! Please help me ! Variables fixed !! Problem exists ! – gimbo Feb 06 '14 at 16:29
  • You should take a look at that link and follow its guidelines. – Paul Samsotha Feb 06 '14 at 16:31
  • can you post your class either via a link to github or any other websites you might have. This way we can try to replicate what you are describing. – grepit Feb 06 '14 at 16:37

1 Answers1

2

The problem seems to be you never remove the filter added to the row sorter when the check box is selected. This way the filter will be working regardless the check box status. You should be doing something like this:

cEntertainment.addItemListener(new ItemListener(){
    public void itemStateChanged(ItemEvent e){
        int state = e.getStateChange();
        if (state == ItemEvent.SELECTED) {
            ...
            newFilter(cEntertainment.getText());
        } else {
            ...
            removeFilter();
        }
    }
});

...

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

private void removeFilter() {
    sorter.setRowFilter(null);
}

As per DefaultRowSorter.setRowFilter(RowFilter filter) javadoc (remarks are mine):

Sets the filter that determines which rows, if any, should be hidden from the view. The filter is applied before sorting. A value of null indicates all values from the model should be included.

dic19
  • 17,821
  • 6
  • 40
  • 69
  • 1
    Mea Culpa (My fault) ! The problem is fixed !! Thank you for the solution and also for the link ! – gimbo Feb 06 '14 at 16:53