javaswingfilterjtableglazedlists

How to replace JTextField as filter with string in GlazedList?


I have a set of radio button which i want to use as filter for my table. this radio button sets a variable in my model class. With a getter in my model i retrieve this value and i want to use this value as filter in my GlazedList table.

Does any body know how to do it?

Below is my table with JTextField as filter:

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

Solution

  • I would point you to the Custom MatcherEditor screencast as a good reference to implementing your own Matchers to cope with filtering from a set of options.

    The key part is the creation of a MatcherEditor, in this instance it's filtering a table of people by nationality.

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

    How this MatcherEditor is used shouldn't be too unfamiliar as it's similar to TextMatcherEditors:

    EventList idols = new BasicEventList();
    NationalityMatcherEditor nationalityMatcherEditor = new NationalityMatcherEditor();
    FilterList filteredIdols = new FilterList(idols, nationalityMatcherEditor);
    

    In the above sample the JComboBox is declared and initiated in the MatcherEditor itself. You need not follow that style exactly, although you need a reference to the object you're tracking. For me, if I'm watching Swing controls I tend to declare and initiate with the rest of the form and then pass a reference in, e.g.

    ....
    private JComboBox nationalityChooser;
    public NationalityMatcherEditor(JComboBox alreadyConfiguredComboBox) {
        this.nationalityChooser = alreadyConfiguredComboBox;
    }
    ....