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);

}

}

}

}

[5021 byte] By [sas_21a] at [2007-11-27 8:25:17]
# 1

The base line is:

to keep rows based on their identifier, implement a custom RowFilter which returns true for those and set the sorters filter to an OrFilter of the custom and the regexFilter.

Some issues to consider:

- keeping the "first" row is most probably only part of your requirement: typically, you want it stick to the top, never get scrolled and not sorted

- it's not really part of the model, so it's a bit dirty hack to artificially include it

- you are mis-using the cell-editing mechanism

You might be better off to separate the filter input fields from the table, f.i. by a custom tableHeader and/or custom scrollpane which manages a line of components below the header. It's not trivial ;-)

Cheers

Jeanette

Kleopatraa at 2007-7-12 20:14:31 > top of Java-index,Desktop,Core GUI APIs...
# 2
Thanks for the reply and to be frank I am not so experienced in this field ,working on swings for the first time and just trying to get things running,but I will surely consider the issues raised by you and work on them , which I am sure will make the practice better for me. Thanks again.
sas_21a at 2007-7-12 20:14:31 > top of Java-index,Desktop,Core GUI APIs...