Add Filter to a JTable columns

Posted on 2003-02-25
Medium Priority
Last Modified: 2007-12-19
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
Question by:jnajayjain
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 86

Expert Comment

ID: 8024484
In what way do you want to filter them?

Expert Comment

ID: 8029930
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 http://java.sun.com/docs/books/tutorial/uiswing/components/table.html#sorting

Hope this helps,

- Eugene

Author Comment

ID: 8031190
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 8031322
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

Accepted Solution

ykaganov earned 120 total points
ID: 8031327
In the constructor, I meant

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

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

- Eugene
LVL 86

Expert Comment

ID: 8032815
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.

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question