开发者

Filtering table with a JCheckBox column

开发者 https://www.devze.com 2023-02-19 04:52 出处:网络
I have a JTable with 6 columns. The first column is a JCheckBox column. I created a search box to filter the table through this code :

I have a JTable with 6 columns. The first column is a JCheckBox column. I created a search box to filter the table through this code :

String searchWord = searchTextField.getText();
TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>( myTable.getModel() );
myTable.setRowSorter( sorter );
sorter.setRowFilter( RowFilter.regexFi开发者_C百科lter( searchWord );

The problem is if user searched for "true", "false", "t", "se", ... it returns the rows having check boxes enabled or disabled ... I guess it deals with their value as true/false.

So is there a way to ignore the first column while filtering the table ?


You can tell the regexFilter which columns you want to check. Say you have five columns in your table, and the first one is the JCheckBox column, then you can ignore it by using:

sorter.setRowFilter( RowFilter.regexFilter( searchWord, 1, 2, 3, 4 ) );

From the docs for RowFilter:

regexFilter

public static <M,I> RowFilter<M,I> regexFilter(String regex, int... indices)

Parameters:

regex - the regular expression to filter on

indices - the indices of the values to check. If not supplied all values are evaluated

0

精彩评论

暂无评论...
验证码 换一张
取 消