简体   繁体   English

Java JTable在两个值之间进行行排序

[英]Java JTable rowsorter between two values

I'm trying to do "live search" between two values in JTable. 我正在尝试在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. 我现在想要做的是这样的事情,但是正如您看到的那样,有两个值“ from-to”,并且仅显示介于最小,最大和等于它们之间的该记录。 Let's say from 150-300. 假设从150-300。 But i have no idea how to do this, i've tried combine 2 filters like 但是我不知道该怎么做,我试过结合2个过滤器

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. 如您所见,我刚刚开始学习Java,对我的英语不好深感抱歉:)干杯。

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. 创建RowFilter.numberFilter(...) ,需要指定要在表中进行比较的列。 The default is all columns if you don't specify an index. 如果不指定索引,则默认为所有列。

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

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