0

我正在Glazedlists使用yaml. 在glazedlists他们提供textfilterator过滤jtable.

现在我想根据 jcombobox 值过滤表格。所以我尝试jcombobox用作我的过滤器。我尝试使用textfilterator. 但它不起作用。我不清楚匹配器。因此,如果有人知道是否有任何filterator.jcombobox

我的代码片段如下:

JPanel(name=ProductPanel,preferredSize=660x400,maximumSize=650x400,minimumSize=650x400):-JPanel(name=insideProductPanel,preferredSize=660x400,maximumSize=660x400,minimumSize=660x400):-JComboBox(name=cmbSearchCategory),onAgo : EventComboBoxModel(source=searchComboList): - JTextField(name=txtSearchProduct): - JScrollPane(name=productScroll,vScrollBar=never,preferredSize=650x400,maximumSize=650x400,minimumSize=650x400): JTable(name=productTable): - EventTableModel( name=productModel,source=productList): - TextFilterator(txtSearchProduct=[name]) - TableColumn(name=id,headerValue="#",preferredWidth=300): - TableColumn(name=productCode,headerValue="code"): - TableColumn(name=name,headerValue="Product"): - TableColumn(name=category,headerValue="Category"):- TableColumn(name=unit,headerValue="UOM"): - TableColumn(name=batchEnabled,headerValue="Batch"): - TableColumn(name=type,headerValue="产品类型"):


- MigLayout: |
[grow]
4

1 回答 1

1

首先,您的示例代码没有意义。它没有任何与实际 Java 代码相似的地方,也没有以任何方式遵守SSCCE原则。

也就是说,您的问题提供了足够的线索来确定您的要求。GlazedLists 确实提供了一个动态过滤列表的框架,这一切都是通过MatcherEditor类完成的。

GlazedLists Developer提供了一些很棒的截屏视频,并且有一个简单的示例准确地处理了您提出的关于如何将 MatcherEditor 与 JComboBox 选择链接起来以触发动态过滤的任务。

此示例的源代码足够短,可以在此处包含:

package ca.odell.glazedlists.example;

import ca.odell.glazedlists.*;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.matchers.AbstractMatcherEditor;
import ca.odell.glazedlists.matchers.Matcher;
import ca.odell.glazedlists.swing.EventTableModel;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class CustomMatcherEditorExample {

    public static class AmericanIdol {
        private String name;
        private int votes;
        private String nationality;

        public AmericanIdol(String name, int votes, String nationality) {
            this.name = name;
            this.votes = votes;
            this.nationality = nationality;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getNationality() {
            return nationality;
        }

        public void setNationality(String nationality) {
            this.nationality = nationality;
        }

        public int getVotes() {
            return votes;
        }

        public void setVotes(int votes) {
            this.votes = votes;
        }

        public void incrementVotes() {
            this.votes++;
        }
    }

    public static void main(String[] args) {
        // create an EventList of AmericanIdol
        final EventList idols = new BasicEventList();
        idols.add(new AmericanIdol("Simon Cowell", 0, "British"));
        idols.add(new AmericanIdol("Paula Abdul", 0, "American"));
        idols.add(new AmericanIdol("Randy Jackson", 0, "American"));
        idols.add(new AmericanIdol("Ryan Seacrest", 0, "American"));

        final NationalityMatcherEditor nationalityMatcherEditor = new NationalityMatcherEditor();
        final FilterList filteredIdols = new FilterList(idols, nationalityMatcherEditor);

        // build a JTable
        String[] propertyNames = new String[] {"name", "votes"};
        String[] columnLabels = new String[] {"Name", "Votes"};
        TableFormat tf = GlazedLists.tableFormat(AmericanIdol.class, propertyNames, columnLabels);
        JTable t = new JTable(new EventTableModel(filteredIdols, tf));

        // place the table in a JFrame
        JFrame f = new JFrame();
        f.setLayout(new BorderLayout());
        f.add(nationalityMatcherEditor.getComponent(), BorderLayout.NORTH);
        f.add(new JScrollPane(t), BorderLayout.CENTER);

        // show the frame
        f.pack();
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }

    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,但上面的示例提供了一个很好的模板。MatcherEditor 的目的是提供逻辑来决定过滤掉什么,或者从技术上讲,为特定输入保留什么。

您的 MatcherEditor 还需要对您希望触发过滤的组件具有某种访问权限。许多示例都将 MatcherEditor 作为特定 Swing 组件的创建者和所有者,但将其传入也同样好。

然后它只是将 MatcherEditor 连接到 FilterList 的一个例子,如果你已经完成了文本过滤,你就会熟悉它。

于 2013-09-06T10:45:38.897 回答