在glazedlists中是否有JComboBox的过滤器

时间:2013-09-02 05:48:12

标签: glazedlists

我正在使用Glazedlists yaml。在glazedlists中,他们提供textfilterator来过滤jtable

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

我的代码段如下:

  

的JPanel(名称= ProductPanel,首选大小= 660x400,MAXIMUMSIZE = 650x400,=的minimumSize 650x400):    - JPanel(name = 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”): -   TableColumn(name = unit,headerValue =“UOM”): -   TableColumn(name = batchEnabled,headerValue =“Batch”): -   TableColumn(name = type,headerValue =“Product of Product”):


- MigLayout: |
[grow]

1 个答案:

答案 0 :(得分: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的情况,如果你已经完成了文本过滤,你会熟悉它。