简体   繁体   English

我怎样才能在两个日期之间创建一个 TableRowShorter

[英]How can i make a TableRowShorter between two Date

combobox.addItemListener(new ItemListener() {
            public void itemStateChanged(ItemEvent arg0) {
                if(arg0.getStateChange()==ItemEvent.SELECTED) {
                    if(!combobox.getSelectedItem().toString().equals(items[0])) { //items[] for the items of the combobox.
                        DefaultTableModel table1 = (DefaultTableModel)table.getModel();
                        String search =combobox.getSelectedItem().toString();
                        TableRowSorter<DefaultTableModel> tr = new TableRowSorter<DefaultTableModel>(table1);
                        table.setRowSorter(tr);
                        tr.setRowFilter(RowFilter.regexFilter(search));
                    }
                }
            }
        });

I use this code.我用这个代码。 When I select an item of the combobox, it does sort the table with selected item.当我 select combobox 的一个项目时,它确实用所选项目对表格进行排序。 I have a second combobox. Let's say combobox's name is combobox2 and item of combobox2 is "Last 2 Month".我有第二个 combobox。假设组合框的名称是 combobox2,combobox2 的项目是“Last 2 Month”。

The first column of the table is for dates.表格的第一列是日期。 When I select the item of combobox2 (Last 2 Month), i want to sort the table.当我 select 组合框 2 的项目(过去 2 个月)时,我想对表格进行排序。 I just want to see rows with maximum 2 months old.我只想查看最多 2 个月大的行。

For example if I sent the String ("01/01/2020"), I only want to see the rows with ("01/01/2020") and after this date.例如,如果我发送字符串 ("01/01/2020"),我只想查看包含 ("01/01/2020") 和此日期之后的行。

But with this code i only can see the date I send.但是使用此代码我只能看到我发送的日期。 I hope I explained it well.我希望我解释得很好。 I can share more code if you guys need.如果你们需要,我可以分享更多代码。

For example if I sent the String ("01/01/2020"), I only want to see the rows with ("01/01/2020") and after this date.例如,如果我发送字符串 ("01/01/2020"),我只想查看包含 ("01/01/2020") 和此日期之后的行。

You need to use a "date filter".您需要使用“日期过滤器”。

For example this filter will return all the date after a specific date:例如,此过滤器将返回特定日期之后的所有日期:

RowFilter.dateFilter(ComparisonType.AFTER, new Date());

Read the RowFilter API for other filters you can use.阅读RowFilter API,了解您可以使用的其他过滤器。

Note:笔记:

You should NOT be storing String values in the model. You should store the date in an object that represents a date and then use a custom renderer to format the date.您不应将字符串值存储在 model 中。您应该将日期存储在表示日期的 object 中,然后使用自定义渲染器来格式化日期。 This will allow the filter from above to work properly.这将使上面的过滤器正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM