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 TableRowSortersorter ;
//
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
jtblSampleTable.setModel(tableModel);
Set the sorter as the row sorter for the table
jtblSampleTable.setRowSorter(sorter);
Now declare a JTextField
jtfSearch = new javax.swing.JTextField();
Add a document listener for the search text field.
jtfSearch.getDocument().addDocumentListener(
new DocumentListener()
{
public void changedUpdate(DocumentEvent e)
{
newFilter();
}
public void insertUpdate(DocumentEvent e)
{
newFilter();
}
public void removeUpdate(DocumentEvent e)
{
newFilter();
}
}
);
Here is the newFilter method
private void newFilter()
{
RowFilter< MyTableModel , Object> rf = null;
//declare a row filter for your table model
Try
{
rf = RowFilter.regexFilter("^" + jtfSearch.getText(), 0);
//initialize with a regular expression
}
catch (java.util.regex.PatternSyntaxException e)
{
return;
}
sorter.setRowFilter(rf);
}
The above example is able to filter JTable for its first column.
20 comments:
Nice Tutorial...
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!
Thanks :D..
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
@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;
nice tuto, i love you XD
Thanks Juan
Good One... thankz
It was really super. easy to understand. Exactly what i needed
Thanks a lot.
Welcome :)
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?
@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.
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 (:
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?
^ is it possible to filter on multiple columns at once?
this was very very helpful! I can't thank you enough for posting this :D
this tutorial is good
very brief and has everything
good job
Very helpful thanks a lot !
Wonderful tutorial.. And very easy to understnad.. Thanks a lot!!!
Good Tutorial. Thanks a lot. It is really helpful. Nice Explanation of each and every step
Post a Comment