java 过滤 JTable 仅一列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5565632/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:41:18  来源:igfitidea点击:

Filter JTable only one one column

javaswingsearchjtable

提问by CMP

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

我有一个 JTable,我想从 a 进行过滤,JTextfield但只过滤基于一列的结果而不是搜索所有列。

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

我有我的 JTable 我可以过滤但我的过滤器将过滤和搜索表的每一列我想将其限制为一列

Can it be done ?

可以做到吗?

回答by kleopatra

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

聆听 textFields 文档中的更改,并将适当的 rowFilter 设置为限制为您要过滤的列:

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

(type-cast and guard against empty textfield ommitted)

(类型转换和防止空文本字段被忽略)

回答by jzd

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.

搜索时,从文本字段中获取搜索条件并通过仅添加符合条件的项目来重建表模型。如果文本字段为空,则添加所有行。

回答by StanislavL

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

您可以像这样扩展信封表模型http://java-sl.com/envelope.html并只留下必要的行。