2

我有一组单选按钮,我想将其用作表格的过滤器。这个单选按钮在我的模型类中设置了一个变量。在我的模型中使用吸气剂,我检索了这个值,我想在我的 GlazedList 表中使用这个值作为过滤器。

有没有人知道怎么做?

下面是我使用 JTextField 作为过滤器的表格:

TextFilterator<Barcode> barcodeFilterator = new TextFilterator<Barcode>() { ... };
    WebTextField searchField = new WebTextField(barcodeModel.getSelectedFilter());
    MatcherEditor<Barcode> textMatcherEditor = new TextComponentMatcherEditor<Barcode>(searchField, barcodeFilterator);
    FilterList<Barcode> filterList = new FilterList<Barcode>(BarcodeUtil.retrieveBarcodeEventList(files), textMatcherEditor);
    TableFormat<Barcode> tableFormat = new TableFormat<Barcode>() { .... };
    EventTableModel<Barcode> tableModel = new EventTableModel<Barcode>(filterList, tableFormat);
    barcodeTable.setModel(tableModel);
4

1 回答 1

2

我会向您指出自定义 MatcherEditor 截屏视频Matcher,作为实现您自己的 s 以应对从一组选项中进行过滤的一个很好的参考。

关键部分是创建 a MatcherEditor,在本例中,它按国籍过滤人员表。

private static class NationalityMatcherEditor extends AbstractMatcherEditor implements ActionListener {
    private JComboBox nationalityChooser;

    public NationalityMatcherEditor() {
        this.nationalityChooser = new JComboBox(new Object[] {"British", "American"});
        this.nationalityChooser.getModel().setSelectedItem("Filter by Nationality...");
        this.nationalityChooser.addActionListener(this);
    }

    public Component getComponent() {
        return this.nationalityChooser;
    }

    public void actionPerformed(ActionEvent e) {
        final String nationality = (String) this.nationalityChooser.getSelectedItem();
        if (nationality == null)
            this.fireMatchAll();
        else
            this.fireChanged(new NationalityMatcher(nationality));
    }

    private static class NationalityMatcher implements Matcher {
        private final String nationality;

        public NationalityMatcher(String nationality) {
            this.nationality = nationality;
        }

        public boolean matches(Object item) {
            final AmericanIdol idol = (AmericanIdol) item;
            return this.nationality.equals(idol.getNationality());
        }
    }
}

MatcherEditor的使用方式应该不会太陌生,因为它类似于TextMatcherEditors:

EventList idols = new BasicEventList();
NationalityMatcherEditor nationalityMatcherEditor = new NationalityMatcherEditor();
FilterList filteredIdols = new FilterList(idols, nationalityMatcherEditor);

在上面的示例中,它本身JComboBox是声明和启动的MatcherEditor。尽管您需要对正在跟踪的对象的引用,但您无需完全遵循该样式。对我来说,如果我正在观看 Swing 控件,我倾向于声明并启动表单的其余部分,然后传入一个引用,例如

....
private JComboBox nationalityChooser;
public NationalityMatcherEditor(JComboBox alreadyConfiguredComboBox) {
    this.nationalityChooser = alreadyConfiguredComboBox;
}
....
于 2012-07-06T09:43:20.107 回答