java - 如何用 GlazedList 中的字符串替换 JTextField 作为过滤器?

标签 java swing filter jtable glazedlists

我有一组单选按钮,我想将其用作表格的过滤器。这个单选按钮在我的模型类中设置一个变量。通过我的模型中的 setter/getter ,我检索该值,并且我想使用该值作为 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);

最佳答案

我会向您指出Custom MatcherEditor screencast作为实现您自己的 Matcher 来处理一组选项中的过滤的一个很好的引用。

关键部分是创建 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 的使用方式应该不会太陌生,因为它与 TextMatcherEditor 类似:

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;
}
....

关于java - 如何用 GlazedList 中的字符串替换 JTextField 作为过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11285090/

相关文章:

java - 在 EJB jar 中,在哪里放置代码以在初始化时只执行一次?

java - 在Java中选择继承还是接口(interface)来实现设计模式?

java - 从 onBalloonTap 加载 Activity

java - 等距游戏鼠标逻辑和环境周围的运动

java - 容器在调用验证后不会自行调整大小

java - 如何替换已弃用的@MockClass?

java - 可编辑 JComboBox KeyPressed 不起作用

matlab - Matlab中高斯滤波器的导数

android过滤器自定义数组适配器并再次带回旧项目

php - datatable:制作服务器端脚本来过滤每一列