简体   繁体   English

GlazedList-仅基于1列的过滤表

[英]GlazedList - Filter table based on 1 column only

I'm using GlazedList for handling JTables in my Swing project implemented using MVC pattern. 我在使用MVC模式实现的Swing项目中使用GlazedList处理JTable。 I have the following code in my controller for incorporating filtering functionality in the table. 我的控制器中包含以下代码,用于在表中合并过滤功能。

    final JTextField txtFilter = view.getTxtSearch();
    FilterList<E> textFilteredSource = new FilterList<E>(model.getDataTableSource(), new TextComponentMatcherEditor<E>(txtFilter, new TextFilterator<E>() {
        public void getFilterStrings(List baseList, E element) {
           Person p = (Person) element;
           baseList.add(p.getFirstName());
           baseList.add(p.getLastName());
           baseList.add(p.getBirthDay());
           baseList.add(p.getAge());
           baseList.add(p.getOccupation());
        }
    }));

model.setDataTableSource(textFilteredSource);

The above code allows my table to filter based on all the data present in the whole table. 上面的代码允许我的表根据整个表中存在的所有数据进行过滤。 What I want is a functionality that only filters the table based on one column only. 我想要的功能是仅基于一个列来过滤表。 Does anybody know how to accomplish this? 有人知道如何做到这一点吗?

Ok so for those who'll encounter the same problem, I solved it myself through experimenting, and I found out that the baseList is actually a list of String where the FilterList will do its filtering job. 好的,对于那些会遇到相同问题的人,我自己通过实验解决了这个问题,然后发现baseList实际上是String的列表, FilterList将在其中进行过滤工作。 To do my requirement, I just added the column value that I need to be filtered to the baseList parameter. 为了满足我的要求,我只是将需要过滤的列值添加到baseList参数中。

The following code will filter the table based on the combobox selected index coming from the view. 以下代码将基于来自视图的组合框选择的索引过滤表。

    public void getFilterStrings(List baseList, E element) {
       JComboBox cbo = view.getCboSearch();
       int selIndex = cbo.getSelectedIndex();
       Person p = (Person) element;

       if(selIndex == 0)
          baseList.add(p.getFirstName());
       else if(selIndex == 1)
          baseList.add(p.getLastName());
       else if(selIndex == 2)
          baseList.add(p.getBirthDay());
       else if(selIndex == 3)
          baseList.add(p.getAge());
       else if(selIndex == 4)
          baseList.add(p.getOccupation());
    }

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

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