is there any filterator for JComboBox in glazedlists

407 Views Asked by At

I am using Glazedlists with yaml. In glazedlists they are providing textfilterator for filtering the jtable.

Now I want to filter the table based on the jcombobox values .So am try to use jcombobox as my filter.I try with the textfilterator. But its not working. I am not clear about the matcher. So if anyone know is there any filterator for jcombobox.

My code snippet is given below :

JPanel(name=ProductPanel,preferredSize=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="Type of Product"):


- MigLayout: |
[grow]
1

There are 1 best solutions below

3
On

Firstly, your example code makes no sense. It doesn't have any semblance of actual Java code and doesn't in any way abide by the SSCCE principle.

That said, your question provides enough clues to ascertain your requirements. GlazedLists does provide a framework for dynamically filtering lists and it's all done via the MatcherEditor class.

There are some great screencasts available at GlazedLists Developer and there's a simple example dealing with precisely the task you raise about how to link up a MatcherEditor with the JComboBox selection in order to trigger dynamic filtering.

The source for this example is short enough to include here:

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());
            }
        }
    }
}

You will need to build your own MatcherEditor for your particular needs, but the example above provides a good template. It's the aim of the MatcherEditor to provide the logic to decide what is filtered out, or technically speaking what stays in for a particular input.

Your MatcherEditor also needs to have some sort of access to the component you wish to trigger the filtering. Many examples have the MatcherEditor as been the creator and owner of the particular Swing component, but it's equally fine to have it passed in.

And then it's just a case of hooking up the MatcherEditor to a FilterList, which you'll be familiar with if you've done text filtering.