I'm using GlazedList for handling JTables in my Swing project implemented using MVC pattern. I have the following code in my controller for incorporating filtering functionality in the table.
final JTextField txtFilter = view.getTxtSearch();
FilterList<E> textFilteredSource = new FilterList<E>(model.getDataTableSource(), new TextComponentMatcherEditor<E>(txtFilter, new TextFilterator<E>() {
public void getFilterStrings(List baseList, E element) {
Person p = (Person) element;
baseList.add(p.getFirstName());
baseList.add(p.getLastName());
baseList.add(p.getBirthDay());
baseList.add(p.getAge());
baseList.add(p.getOccupation());
}
}));
model.setDataTableSource(textFilteredSource);
The above code allows my table to filter based on all the data present in the whole table. What I want is a functionality that only filters the table based on one column only. Does anybody know how to accomplish this?
Ok so for those who'll encounter the same problem, I solved it myself through experimenting, and I found out that the baseList
is actually a list of String where the FilterList
will do its filtering job. To do my requirement, I just added the column value that I need to be filtered to the baseList
parameter.
The following code will filter the table based on the combobox selected index coming from the view.
public void getFilterStrings(List baseList, E element) {
JComboBox cbo = view.getCboSearch();
int selIndex = cbo.getSelectedIndex();
Person p = (Person) element;
if(selIndex == 0)
baseList.add(p.getFirstName());
else if(selIndex == 1)
baseList.add(p.getLastName());
else if(selIndex == 2)
baseList.add(p.getBirthDay());
else if(selIndex == 3)
baseList.add(p.getAge());
else if(selIndex == 4)
baseList.add(p.getOccupation());
}