简体   繁体   中英

Java JTable rowsorter between two values

I'm trying to do "live search" between two values in JTable. Let's say this is my table:

表

I've done normal search with something like this

    jTextField1.getDocument().addDocumentListener(new DocumentListener() {
        @Override
        public void insertUpdate(DocumentEvent e) {
            String search = jTextField1.getText();

            if (search.trim().length() == 0) {
                rowSorter.setRowFilter(null);                    
            } else {
                rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + search));
            }

        }
        @Override
        public void removeUpdate(DocumentEvent e) {
            String search = jTextField1.getText();

            if (search.trim().length() == 0) {
                rowSorter.setRowFilter(null);
            } else {
                rowSorter.setRowFilter(RowFilter.regexFilter("(?i)" + search));
            }

        }
        @Override
        public void changedUpdate(DocumentEvent e) {
            throw new UnsupportedOperationException("Not supported yet."); 
        }
    }); `

What i want now is to do something like this but as you can see with two values 'from-to' and display only this records with are between min, max and equal to them. Let's say from 150-300. But i have no idea how to do this, i've tried combine 2 filters like

List<RowFilter<Object,Object>> filters = new ArrayList<RowFilter<Object,Object>>(2);
filters.add( RowFilter.numberFilter(ComparisonType.AFTER, Float.parseFloat(min)) );
filters.add( RowFilter.numberFilter(ComparisonType.BEFORE, Float.parseFloat(max)) );
RowFilter<Object, Object> rf = RowFilter.andFilter(filters);
rowSorter.setRowFilter(rf);

but this is not working. Is anyone have idea how to do this? I'm starting to give up... aha my data in table are Objects and I'm using default table model. As you can see I'm just started learning java and sorry for my bad english :) Cheers.

filters.add( RowFilter.numberFilter(ComparisonType.AFTER, Float.parseFloat(min)) );
filters.add( RowFilter.numberFilter(ComparisonType.BEFORE, Float.parseFloat(max)) );

When creating the RowFilter.numberFilter(...) you need to specify which column in the table you want to do your comparison on. The default is all columns if you don't specify an index.

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