Add Filter to a JTable columns

Dear Sir/Madam,

i need to add the filter to JTable columns and also want to do the poperations of filtering. What should i do ?

ur help 'll  be of my greate favour!!!
thanx in advance ..

Best Regards
Who is Participating?
ykaganovConnect With a Mentor Commented:
In the constructor, I meant

 _filters = new Filter[realModel.getColumnCount()];

I'm pseudocoding, so there are probably other errors too :)

- Eugene
In what way do you want to filter them?
If you just want to hide some columns, the easiest (although not the best) way to do it is:

int colIndex = 0; // whatever column it is
TableColumn column = myTable.getColumnModel().getColumn(colIndex);

If you're asking about filtering ROWS, then the approach would be the same as if you wanted to sort them. You want to create a "proxy" table model that keeps track of the indices to the original model's rows. Take a look at

Hope this helps,

- Eugene
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

jnajayjainAuthor Commented:
thanx for replying...

But this doesn't solve my problem.Actually i want to implement the filter operation as implemented in the MS-Excel sheet. I want to achive the following objectives.

1) The topmost row of a selected column should change to a combo box where some filter condition exist as  combobox's items.
2) On selecting any of those condition ,only those rows should be displayed which satisfy the  conditions.

im using combobox as a cell editor for this. I have using  a popup menu  
   //if auto filter menu item is clicked
   if (e.getSource().getText().equals("Auto Filter")){
         //gets the index of selected column    
          int index = table.getSelectedColumn();
             JComboBox combobox  = new JComboBox();
             combobox.addItem("Top 10");
             combobox.addItem("Last 10");
 table.getColumnModel().getColumn(index).setCellEditor(new DefaultCellEditor(combobox));

this code activate the editor for whole column and i want it only for the first row.

hope u have understood my problem

ajay jain
I still think creating a proxy table model will work. Build an index for the rows allowed by the current set of filters.

public interface Filter {
  boolean allow();

public class FilterTableModel {
 Filter[] _filters;
 Hashtable _indexHash = new Hashtable();

 public FilterTableModel(TableModel realModel) {
  _realModel = realModel;
  _filters = realModel.getColumnCount();
 public setFilterForColumn(Filter filter) {
 public void getValueAt(int row, int col) {
  int realRow = _indexHash.get(row);
  return _realModel.getValueAt(realRow, col);
 protected void rebuildIndexHash() {
   ... put logic here

In rebuildIndexHash(), iterate over all rows and all columns, and set the index of a proxy row to the index of a real row that satisfies all the filters.

I could try to write the complete class for you, but not for 40 points :)


- Eugene
One thing that's going to make things easier for you is to forget about the cell editors at first and get the filtering working. You can put the combo stuff in later.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.