我如何在两个日期之间创建一个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 []用于组合框的项目。
DefaultTableModel table1 =(DefaultTableModel)table .getModel();
字符串搜索= combobox.getSelectedItem()。toString();
TableRowSorter< DefaultTableModel> tr =新TableRowSorter< DefaultTableModel>(table1);
table.setRowSorter(tr );
tr.setRowFilter(RowFilter.regexFilter(search));
}
}
}
});
我使用此代码。当我选择组合框的一个项目时,它会将所选项目与表格进行排序。我有第二个组合框。假设combobox的名称为combobox2,combobox2的项目为过去2个月。
表格的第一列是日期。当我选择combobox2的项目(过去2个月)时,我想对表格进行排序。我只想查看最长使用2个月的行。
例如,如果我发送了字符串( 01/01/2020),我只想查看带有( " 01/01/2020")之后的日期。
但是使用此代码,我只能看到我发送的日期。我希望我能解释清楚。如有需要,我可以共享更多代码。
例如,如果我发送了String( " 01/01/2020"),我只想查看该日期之后带有(" 01/01/2020")的行。
您需要使用日期过滤器。
例如,此过滤器将返回特定日期之后的所有日期:
RowFilter.dateFilter(ComparisonType.AFTER,新的Date());
阅读 RowFilter
API,了解可以使用的其他过滤器。 / p>
注意:
您不应在模型中存储字符串值。您应该将日期存储在代表日期的对象中,然后使用自定义渲染器格式化日期。这将使上方的过滤器正常工作。
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. I have a second combobox. Let's say combobox's name is combobox2 and item of combobox2 is "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. I just want to see rows with maximum 2 months old.
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.
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.
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.
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. This will allow the filter from above to work properly.
这篇关于我如何在两个日期之间创建一个TableRowShorter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!