简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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