如何在两个日期之间创建一个更短的TableRow?

huangapple go评论74阅读模式
英文:

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])) {
                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));
            }
        }
    }
});

我使用这段代码。当我选择组合框的项目时,它会根据所选项目对表格进行排序。我有第二个组合框。假设组合框的名称是combobox2,combobox2的项目是"Last 2 Month"。

表格的第一列是日期。当我选择combobox2的项目(Last 2 Month)时,我想对表格进行排序。我只想看到最多2个月之前的行。

例如,如果我发送字符串("01/01/2020"),我只想看到("01/01/2020")和此日期之后的行。

但是,使用这段代码,我只能看到我发送的日期。希望我解释清楚了。如果需要,我可以分享更多代码。

英文:
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&lt;DefaultTableModel&gt; tr = new TableRowSorter&lt;DefaultTableModel&gt;(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.

答案1

得分: 2

> 例如,如果我发送了字符串("01/01/2020"),我只想看到("01/01/2020")及之后的行。

你需要使用一个"日期过滤器"。

例如,这个过滤器将返回所有在特定日期之后的日期:

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

阅读RowFilter API以了解其他可用的过滤器。

注意:

您不应该将字符串值存储在模型中。您应该将日期存储在表示日期的对象中,然后使用自定义渲染器来格式化日期。这将使上述过滤器能够正常工作。

英文:

> 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.

huangapple
  • 本文由 发表于 2020年8月19日 20:48:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/63487326.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定