Filter a JTable row with input in a text field

In an enterprise application when there is huge amount data in a table
users feel comfortable if you provide them a option for filtering the
table. Here I show you an example of how to filter a JTable row with
input in a JTextField.

Declare a JTable and your own table model

private MyTableModel tableModel;
private javax.swing.JTable jtblSampleTable;

Now declare a table row sorter for that table model

private TableRowSorter sorter ;

Initialize all the above three declared variables

jtblSampleTable = new javax.swing.JTable();
tableModel = new MyTableModel();
sorter = new TableRowSorter(tableModel);

Bind the model with the table


Set the sorter as the row sorter for the table


Now declare a JTextField

jtfSearch = new javax.swing.JTextField();

Add a document listener for the search text field.

new DocumentListener()
public void changedUpdate(DocumentEvent e)
public void insertUpdate(DocumentEvent e)
public void removeUpdate(DocumentEvent e)

Here is the newFilter method

private void newFilter()
RowFilter< MyTableModel , Object> rf = null;
//declare a row filter for your table model
rf = RowFilter.regexFilter("^" + jtfSearch.getText(), 0);
//initialize with a regular expression
catch (java.util.regex.PatternSyntaxException e)

The above example is able to filter JTable for its first column.


Maksud March 7, 2009 at 6:46 AM  

Nice Tutorial...

Anonymous November 23, 2009 at 8:29 AM  

Agreed i was getting all sorts of confused with all the examples on the web so its nice that some1 has taken the time to actually go through, even as briefly as u did and explain what we do at stage. Thanks again =D!

Unknown November 23, 2009 at 6:51 PM  

Thanks :D..

Unknown January 16, 2010 at 2:17 PM  

Good tutorial, but i spend the last 40 min trying to find the right inport statements.

So this doesnt work..

If i find "iport java.bla.*" ill post them here

Unknown January 16, 2010 at 9:10 PM  

@Evelien: I think most of modern IDE can fix the import problem. If you are using Netbeans just press Ctrl+Shift+i and for Eclipse press Ctrl+Shift+o, it will fix the import problem. The correct import statement will be like this:

import javax.swing.RowFilter;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;

Juan Alberto June 7, 2010 at 9:05 PM  

nice tuto, i love you XD

Unknown June 7, 2010 at 9:10 PM  

Thanks Juan

CrushOnJava November 22, 2010 at 1:10 AM  

Good One... thankz

Ali November 23, 2010 at 4:43 AM  

It was really super. easy to understand. Exactly what i needed
Thanks a lot.

Unknown November 23, 2010 at 6:15 AM  

Welcome :)

FuMei July 24, 2011 at 7:45 AM  

Hi there!

I noticed that the filtering works only for the first column, as in when I tried typing characters that appear in other columns but not in the first, there are no results shown. Is there a way to filter from any of the columns?

Unknown July 24, 2011 at 6:37 PM  

@FuMei: yes you are right. filtering only works for the first folumn. If you want to filter by more than one column then change this line

rf = RowFilter.regexFilter("^" + jtfSearch.getText(), 0,1,2);

it is a variable length parameter. you can put as many number as you want.

FuMei July 25, 2011 at 8:08 AM  

Ohhh thanks! (;

Ohoh I found out another thing too! If you just do this:

rf = RowFilter.regexFilter(jtfSearch.getText());

it actually filters off from any column (:

Anonymous April 10, 2012 at 12:46 PM  

This tutorial is fantastic. Now I'm wondering if it's possible to have 2 such filters with the second working only on the result set that was filtered in the first.
So if I filter first on 'A' and get:
col1 col2
A 1
A 2
A 3
..could you have a second text box to filter for col2?

Anonymous April 10, 2012 at 12:51 PM  

^ is it possible to filter on multiple columns at once?

lillamig May 8, 2012 at 4:12 PM  

this was very very helpful! I can't thank you enough for posting this :D

EdgerLyamba May 18, 2012 at 4:40 PM  

this tutorial is good
very brief and has everything

good job

Unknown July 28, 2012 at 1:19 PM  

Very helpful thanks a lot !

JavaBeginner August 15, 2013 at 1:06 AM  

Wonderful tutorial.. And very easy to understnad.. Thanks a lot!!!

Unknown September 14, 2013 at 11:01 PM  

Good Tutorial. Thanks a lot. It is really helpful. Nice Explanation of each and every step

Total Pageviews


Twitter Updates
    follow me on Twitter