Java 基于表模型行整数列表的JTable行过滤器
我正在为我的数据表构建一个查询屏幕。我已经完成了工作,并根据用户条件合并了要在表上显示的行 如何基于模型的行将行筛选器应用于表?例如:我想显示存储在ArrayList列表中的第3、6、7、8、9行 我目前没有掌握RowFilter类。我不知道在这里做什么或如何使用它:Java 基于表模型行整数列表的JTable行过滤器,java,swing,jtable,rowfilter,Java,Swing,Jtable,Rowfilter,我正在为我的数据表构建一个查询屏幕。我已经完成了工作,并根据用户条件合并了要在表上显示的行 如何基于模型的行将行筛选器应用于表?例如:我想显示存储在ArrayList列表中的第3、6、7、8、9行 我目前没有掌握RowFilter类。我不知道在这里做什么或如何使用它: RowFilter<DefaultTableModel,Integer> rf = new RowFilter<>() { @Override public boolean include(
RowFilter<DefaultTableModel,Integer> rf = new RowFilter<>() {
@Override
public boolean include(Entry entry) {
// TODO Auto-generated method stub
return false;
}
};
也许您会发现下面提供的方法有些有用。我用它来过滤JTables。您需要做的是使用TableRowSorter类:
/**
* Make sure the supplied JTable's DefaultTableModel already contains data
* before calling this method.
* <p>
* The JTable always retains the data it is currently filled with within its
* Table Model. <b>Knowing this we can theoretically fill the Table with all
* database table records then use the Filter to display only the specific
* table records we want.</b> This way we don't have to pole the database
* for different records all the time and records acquisition is greatly
* increased.
* <p>
* This method will pass the supplied Filter text across the supplied JTable
* and will force that JTable to only display records (contained within that
* JTable at the time) which contains that specific text. It reacts very
* much like a search engine for the JTable.
* <p>
* If you want to display all records again which were originally contained
* within the supplied JTable then just pass a Null String ("") within the
* searchCriteria parameter of this method.
*
* @param table (JTable) The JTable to run filter on.<br>
*
* @param searchCriteria (String) The text to filter JTable with. Passing a
* Null String ("") will force the table to display
* all records. Regular Expressions (RegEx) can also
* be supplied within the criteria string. If the
* wildcard characters <b>?</b> or <b>*</b> are
* supplied within the filter criteria String without
* any RegEx meta characters then the functional
* purpose of these two wildcard characters are
* converted to RegEx when encountered. If actual
* Regular Expressions are going to be used to make up
* the search criteria string then be sure to set the
* <b>endorseWildcards</b> optional parameter to
* boolean false since the <b>?</b> and <b>*</b>
* wildcard characters have different meaning within a
* Regular Expression and must be handled
* differently.<br>
*
* @param options (optional - Integer/Boolean):<pre>
*
* byColumnNumber - (Optional - Integer - Default is -1) By default
* this method filters across all table columns but
* you can be column specific if you pass a column
* number to this optional parameter. This parameter
* accepts only a <b>Literal Column Number</b> which
* means that although the first column within a
* JTable is considered column 0, to this method it is
* considered as column 1.
*
* endorseWildcards - (boolean) Default is true (allow wildcards). If true
* then when a wildcard character is encountered within
* a search criteria string it is automatically converted
* to its RegEx equivalent. The two wildcard characters
* are almost always more than enough to carry out any
* search required and is usually much easier to use than
* some complex regular expressions. If endorseWildcards
* is true then upper or lower letter case is ignored as
* well.
*
* If you provide a true of false to this parameter then
* you must provide a value (or null) to the option
* <b>byColumnNumber</b> parameter.</pre>
*/
public static void filterTable(JTable table, String searchCriteria, Object... options) {
int column = -1;
boolean endorseWildcards = true;
if (options.length > 0) {
if (options[0] != null) {
column = (int) options[0] - 1;
}
if (options.length >= 2) {
if (options[1] != null) {
endorseWildcards = (boolean) options[1];
}
}
}
String criteria = searchCriteria;
if (endorseWildcards) {
criteria = "(?i)" + searchCriteria.replace("?", ".?").replace("*", ".*?");
}
try {
TableRowSorter<TableModel> sorter = new TableRowSorter<>(((DefaultTableModel) table.getModel()));
sorter.setRowFilter(column < 0 ? RowFilter.regexFilter(criteria)
: RowFilter.regexFilter(criteria, column));
table.setRowSorter(sorter);
}
catch (Exception ex) {
ex.printStackTrace();
}
}
在学习并试图掌握RowFilter类是如何工作的之后,我让我的表按照我希望的方式进行排序。我仍然需要做更多的测试,但它看起来是工作。我仍然没有完全掌握RowFilter类,因此我希望得到一些反馈 这是我的代码:其中ArrayList filteredRows=new ArrayList;包含我要显示的行号。据我所知,过滤器会自动迭代我的表模型,标识符是过滤器当前正在处理的行号。因此,乍一看,如果标识符等于我存储的任何行号,则显示它
如何基于模型的行将行筛选器应用于表?-根据模型中找到的数据进行筛选。我想显示第3、6、7、8、9行-您是如何决定只显示这些行的?这是应该作为过滤器的一部分的逻辑。@camickr基于构建查询的用户,我循环了数据集,并将数据与查询中的条件进行了匹配。最后,我返回一个布尔值,告诉您是否应该将该行添加到要显示的行列表中。看来我是在过滤器外工作的,这就是我的问题。有没有什么方法可以让我的情况在不重做我的工作的情况下发挥作用?我还没有机会看到德维尔森德的答案
RowFilter<DefaultTableModel,Integer> rf = new RowFilter<>() {
@Override
public boolean include(Entry<? extends DefaultTableModel, ? extends Integer> entry) {
int entryRow = entry.getIdentifier();
for (Integer i : filteredRows) {
if (entryRow == i) return true;
}
return false;
}
};
TableRowSorter<DefaultTableModel> sorter = new TableRowSorter<DefaultTableModel>(myTableModel);
sorter.setRowFilter(null);
sorter.setRowFilter(rf);
table.setRowSorter(sorter);