【问题标题】:is there any filterator for JComboBox in glazedlistsglazedlists 中是否有用于 JComboBox 的过滤器
【发布时间】:2013-09-02 05:48:11
【问题描述】:

我正在使用Glazedlistsyaml。在glazedlists 中,他们提供textfilterator 用于过滤jtable

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

我的代码 sn-p 如下:

JPanel(name=ProductPanel,preferredSize=660x400,maximumSize=650x400,minimumSize=650x400): - JPanel(名称=insideProductPanel,preferredSize=660x400,maximumSize=660x400,minimumSize=660x400): - JComboBox(name=cmbSearchCategory,onAction=searchCategory): 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"): - 表列(名称=单位,headerValue="UOM"):- TableColumn(name=batchEnabled,headerValue="Batch"): - TableColumn(name=type,headerValue="产品类型"):


- MigLayout: |
[grow]

【问题讨论】:

    标签: glazedlists


    【解决方案1】:

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

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

    GlazedLists Developer 提供了一些很棒的截屏视频,simple example 正是处理您提出的关于如何将 MatcherEditor 与 JComboBox 选择链接起来以触发动态过滤的任务。

    此示例的source 足够短,可以在此处包含:

    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 的一个例子,如果你已经完成了文本过滤,你就会熟悉它。

    【讨论】:

    • 感谢您的回复..实际上您的示例对我有用。但我想提一件事,您在上面看到的代码不是 java 代码,它是 yaml 中的设计代码。所以它没有java代码相似度。
    • @user2668129 好的,但是您最初的问题在几个层面上没有意义:1)yaml 的格式不正确 2)yaml 本身并不是创建 Swing 接口的典型方式,您是大概使用了一些可以理解您的 yaml 代码但您没有提及它的第三方库。 3) GlazedLists 不是可以在您的 yaml 设计中声明的 Swing 组件。因此,您应该使用 Java 代码来说明您是如何尝试将 JComboBox 连接到 GlazedLists 的。
    • 如果你知道这么多东西,那你为什么不区分yaml代码和java代码呢??
    猜你喜欢
    • 1970-01-01
    • 2012-05-21
    • 2015-02-06
    • 2012-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    相关资源
    最近更新 更多