java - 如何将两个不同级别的过滤器添加到同一个 GlazedList 表中?

标签 java swing filter glazedlists

我创建了一个表,我想在两个不同的级别对其进行过滤。 首先使用文件扩展名上的单选按钮过滤它,例如(.jpg,.doc,其余)。 第二次使用 textField 对其进行过滤,以搜索第一个过滤中的内容。 如下所示,您可以在演示中看到我可以使用单选按钮过滤表格,但我不知道如何在表格上应用第二级过滤器(JTextField)。

有人知道怎么做吗?

enter image description here

public class TwoLevelFilterTablePanel extends OeVubPanel {
private static final File DirectoryEngine = new File("C:\\Users\\Public\\Pictures\\Sample Pictures");
private JRadioButton jpg,doc,others;
private ButtonGroup radioSet;
private JTextField txtFilter;
private JTable glazedTable;
private BasicEventList<MyCode> eventList;
private JPanel filterPanel,radioSetPanel;

public TwoLevelFilterTablePanel(BasicEventList<MyCode> eventList) {
    this.eventList=eventList;
    createComponents();
    layoutComponents();
}

public void createComponents() {
    jpg = new JRadioButton("jpg");
    doc= new JRadioButton("doc");
    others= new JRadioButton("other");
    radioSet = new ButtonGroup();
    radioSet.add(jpg);
    radioSet.add(doc);
    radioSet.add(others);
    txtFilter = new JTextField();

    radioSetPanel = new JPanel();
    radioSetPanel.setLayout(new BoxLayout(radioSetPanel, BoxLayout.X_AXIS));
    radioSetPanel.add(jpg);
    radioSetPanel.add(doc);
    radioSetPanel.add(others);
    filterPanel=new JPanel();
    filterPanel.setLayout(new BoxLayout(filterPanel, BoxLayout.Y_AXIS));
    filterPanel.add(radioSetPanel);
    filterPanel.add(txtFilter);

    final BarcodeMatcherEditor barcodeMatcherEditor = new BarcodeMatcherEditor(jpg,doc,others);
    final FilterList filteredRadioSet = new FilterList(eventList, barcodeMatcherEditor);

    // build a JTable
    String[] propertyNames = new String[] {"name", "size","date"};
    String[] columnLabels = new String[] {"Name", "Size","Date"};
    TableFormat tf = GlazedLists.tableFormat(MyCode.class, propertyNames, columnLabels);
    glazedTable = new JTable(new EventTableModel(filteredRadioSet, tf));
}

public void layoutComponents() {
    setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
    add(filterPanel);
    add(new WebScrollPane(glazedTable));
}

public static void main(String[] args) {
    BasicEventList<MyCode> eventList  = new BasicEventList<MyCode>();
    for(File file : DirectoryEngine.listFiles()){
        eventList.add(new MyCode(file.getName(),file.length(),new Date(file.lastModified())));
    }
    TwoLevelFilterTablePanel demo = new TwoLevelFilterTablePanel(eventList );
    JFrame frame = new JFrame();
    Container cp = frame.getContentPane();
    cp.add(demo);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(500, 500);
    frame.setLocation(500, 500);
    frame.setVisible(true);
}
} 

MatcherEditor 类:

public class BarcodeMatcherEditor extends AbstractMatcherEditor implements ActionListener {
    private JRadioButton jpg,doc,others;
    public BarcodeMatcherEditor(JRadioButton jpg, JRadioButton doc, JRadioButton others) {
        this.jpg = jpg;
        this.doc=doc;
        this.others =others;
        this.jpg.addActionListener(this);
        this.doc.addActionListener(this);
        this.others.addActionListener(this);
    }

    public void actionPerformed(ActionEvent e) {
        final String filter = ((JRadioButton)e.getSource()).getText();
        if (filter == null)
            this.fireMatchAll();
        else
            this.fireChanged(new BarcodeFilterMatcher(filter));
    }

    private static class BarcodeFilterMatcher implements Matcher {
        private final String filter;
        public BarcodeFilterMatcher(String filter) {
            this.filter = filter;
        }
        public boolean matches(Object item) {
            final MyCode code = (MyCode) item;
            return return filter.equals("other") || code.getName().endsWith(this.filter);
        }
    }
}

最佳答案

您只需将两个 FilterList 链接在一起即可:

EventList<Person> personLists = ...
...
FilterList<Person> filterListByGender = new FilterList<Person>(personList, genderMatchEditor);
FilterList<Person> filterListBySurname = new FilterList<Person>(filterByGender, textSurnameMatchEditor);
// Continue using the filterListBySurname as you usually would
...

关于java - 如何将两个不同级别的过滤器添加到同一个 GlazedList 表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11412377/

相关文章:

java - ListFragments 不会自动恢复数据集吗?

ios - 比较值并在 uitableview 中显示特定结果

php - 锂通用型号过滤器

java - 如何使用捕捉到道路在道路 api 中请求?

java - Android 中的重写 onResume 方法不适用于每个 fragment

java - 如何将列表中的多种类型传递给函数?

java - 在 Graphics2D 中旋转动画线 (Java)

java - 取消 Java Swing 输入窗口中的更改

java - Java条件中的 boolean 值

javascript - Angular ui-select 只过滤一个字段