8

I have a JTable and I would like to do a filter from a JTextfield but only filter the results based on one column and not to search all columns.

I have my JTable I can filter but my filter will filter and search every column of the table I want to restrict it to one column

Can it be done ?

jzd
  • 23,473
  • 9
  • 54
  • 76
CMP
  • 435
  • 3
  • 8
  • 22

3 Answers3

15

listen to changes in the textFields document and set the appropriate rowFilter limited to the column you want to filter:

 // on document change
 RowFilter rowFilter = RowFilter.regexFilter(textField.getText(), myColumn);
 table.getRowSorter().setRowFilter(rowFilter);

(type-cast and guard against empty textfield ommitted)

kleopatra
  • 51,061
  • 28
  • 99
  • 211
1

Keep a master copy of all the rows (or supporting data) in the background.

When searching, take the search criteria from the text field and rebuild the table model by only adding items that match the criteria. If the text field is empty then add all rows.

jzd
  • 23,473
  • 9
  • 54
  • 76
  • good (though not trivial to get right) for pre1.6, not since 1.6: my general rule is to always use all the support available by any framework, the remainder is hard enough ;-) – kleopatra Apr 06 '11 at 12:01
1

You can extend envelope table model like this http://java-sl.com/envelope.html and leave only necessary rows.

StanislavL
  • 56,971
  • 9
  • 68
  • 98