I have a List
, that included JPA Entity
objects of a certain type. Their reference
String values are displayed in a JList
for the user to see.
I want my user to be able to select filters as JCheckBoxes in the UI such as 'only from Client x', or 'only of Type x' and dynamically filter the Entity List.
I had thought to just keep a copy of static List completeList;
and static List filteredList;
and then just run individual filter methods each time a new filter is selected in the UI to update filteredList
, which would work fine until you have to un-select a single filter and leave the others selected (at which point it all falls apart).
Every situation I think through fall apart at one point or another, usually when trying to select multiple filters of from one Menu.
An example of my thought pattern that checks all the filters to determine what needs to go in the new JList;
public static void filterList(){
List filteredList = new ArrayList<Job>(StoredDataClass.completeList);
if(clientSmithsCheckBox.isSelected()){
for(Job job : filteredList){
if(!job.getClient.equals(clientSmithsCheckBox.getText())){
filteredList.remove(job);
}
}
}
....... // Check other filters here etc.
if(clientBobAndCoCheckBox.isSelected()){
for(Job job : filteredList){
if(!job.getClient.equals(clientBobAndCoCheckBox.getText())){
filteredList.remove(job);
}
}
}
Even if clientBobAndCoCheckBox is selected, no jobs with that client will show in the final list, because we already removed them all because another client was already selected. Now, we could add to the list instead but we would face similar problems of having add stuff that shouldn't be there etc.
This is obviously possible, because this type of filtering system is common practice (example, excel). Although this is more of a design question, how can I achieve this?