Filtering in JTable
Hello,
I have a requirement to use the filter functionality in a JTable. But ,unlike the examples i have seen on the sites i have to set the first row as the filterline where i enter some text to filter the rest of the rows, based on this text.for every column.This filter part is working fine not a problem...but once the filtering is done I want thefirst row to remain with the text used for filtering and not occupied by the filtered rows...I mean the filtered rows should be displayed from the 2nd row and not from the first row..first row is meant just to enter the text for filtering and at the same time sorting should also be starting from 2nd row and not from the first row...here is my code for filtering ..Please help..
public MyTable(String[] columns, Object[][] dataArray){
MyTableModel myTableModel =new MyTableModel(columns, dataArray);
rowSorter =new TableRowSorter<MyTableModel>(myTableModel);
JTable table =new JTable(myTableModel){
public Component prepareRenderer(TableCellRenderer renderer,int row,int column){.....}
table.setRowSorter(rowSorter);
int rows = table.getModel().getRowCount();
int cols = table.getModel().getColumnCount();
field=new JTextField(20);
for(int i=0; i< cols;i++){
TableColumn col = table.getColumnModel().getColumn(i);
col.setCellEditor(new DefaultCellEditor(field));
}
field.addActionListener(new ActionListener(){
publicvoid actionPerformed(ActionEvent e){
int cols = table.getModel().getColumnCount();
field.getText();
System.out.println("ActioPerformed:"+field.getText());
newFilter();
}
});
field.getDocument().addDocumentListener(new DocumentListener(){
publicvoid changedUpdate(DocumentEvent e){
field.getText();
newFilter();
}
publicvoid insertUpdate(DocumentEvent e){
field.getText();
System.out.println("insertUpdate:"+field.getText());
//newFilter();
}
publicvoid removeUpdate(DocumentEvent e){
field.getText();
System.out.println("removeUpdate:"+field.getText());
newFilter();
}
});
topPanel.add( scrollPane, BorderLayout.CENTER );
}
privatevoid newFilter(){
int cols = table.getModel().getColumnCount();
int viewRow = table.getSelectedRow();
if(viewRow==0){
//String text = field.getText();
if (field.getText().length() == 0){
rowSorter.setRowFilter(null);
}else{
RowFilter<MyTableModel, Object> rf =null;
try{
rf = RowFilter.regexFilter(field.getText());
}catch (java.util.regex.PatternSyntaxException e){
return;
}
rowSorter.setRowFilter(rf);
rowSorter.convertRowIndexToView(viewRow);
}
}
}
}

