Java GlazedList individual column filtering

232 Views Asked by At

I currently have a table with a textfield in the individual column headers that does filtering based on each of their individual columns.. I am trying to use glazed list instead but how do I do it? each column should have an individual textfield and filtering is done based on all the textfields which filters based on individual columns. E.G: Table with column "First name" and "Last name". The table will filter results based on person with first name based in the firstname filter and with last names based on the last name filter

1

There are 1 best solutions below

2
On

The first thing you need to get your head around is that GlazedLists is primarily about handling lists of objects -- the clue is in the name. Many people approach it as a library for adding sorting & filtering capabilities to tables and lists, which is guaranteed to cause headaches.

So focus first on the fact that GlazedLists will provide you with a special type of list structure called an EventList, which is your core data structure; from there you will be provided with useful methods for transforming, sorting, filtering etc. And then, because it's super-generous, there are easy to use classes to link up your EventList to a JList or JTable.

Once this is all wired up, changes and transformations to the list will automatically propagate to any Swing components which are linked to it.

Anyway, here's a sample class which will show you how you create an EventList, then to apply text filters via a Swing component, and then to link it all up to a Swing table. (This doesn't cover sorting, or how to obtain selected items, but the docs are excellent.)

Tested with Java 9 and GlazedLists 1.11.

import ca.odell.glazedlists.*;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.matchers.MatcherEditor;
import ca.odell.glazedlists.swing.DefaultEventSelectionModel;
import ca.odell.glazedlists.swing.DefaultEventTableModel;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;

import javax.swing.*;
import java.awt.*;
import java.util.List;

public class GlazedListsMultipleTextfields {

    private JFrame frame;
    private JTable table;
    private JTextField txtFirstName;
    private JTextField txtLastName;

    private EventList people;
    private DefaultEventSelectionModel selectionModel;

    public GlazedListsMultipleTextfields() {

        setupGui();
        setupGlazedLists();
        populatedList();
        frame.setVisible(true);
    }

    private void setupGui() {

        frame = new JFrame("GlazedLists Multiple Filter Examples");
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        // Create the panel to hold the input textfields
        txtFirstName = new JTextField();
        JPanel pnlFirstName = new JPanel(new BorderLayout());
        pnlFirstName.add(new JLabel("First name"), BorderLayout.WEST);
        pnlFirstName.add(txtFirstName, BorderLayout.CENTER);

        txtLastName = new JTextField();
        JPanel pnlLastName = new JPanel(new BorderLayout());
        pnlLastName.add(new JLabel("Last name"), BorderLayout.WEST);
        pnlLastName.add(txtLastName, BorderLayout.CENTER);

        JPanel textInputs = new JPanel();
        textInputs.setLayout(new BoxLayout(textInputs, BoxLayout.Y_AXIS));
        textInputs.add(pnlFirstName);
        textInputs.add(pnlLastName);

        table = new JTable();
        frame.getContentPane().add(new JScrollPane(table), BorderLayout.CENTER);
        frame.getContentPane().add(textInputs, BorderLayout.NORTH);
    }

    private void populatedList() {
        people.add(new Person("John", "Grisham"));
        people.add(new Person("Patricia", "Cornwell"));
        people.add(new Person("Nicholas", "Sparks"));
        people.add(new Person("Andy", "Weir"));
        people.add(new Person("Elizabeth", "George"));
        people.add(new Person("John", "Green"));
    }

    private void setupGlazedLists() {
        people = new BasicEventList();
        MatcherEditor firstNameMatcherEditor = new TextComponentMatcherEditor(txtFirstName, new FirstNameTextFilterator());
        MatcherEditor lastNameMatcherEditor = new TextComponentMatcherEditor(txtLastName, new LastNameTextFilterator());

        FilterList filteredFirstNames = new FilterList(people, firstNameMatcherEditor);
        FilterList filteredLastNames = new FilterList(filteredFirstNames, lastNameMatcherEditor);

        TableFormat tableFormat = GlazedLists.tableFormat(new String[]{"firstName", "lastName"}, new String[]{"First Name", "Last Name"});
        DefaultEventTableModel model = new DefaultEventTableModel(filteredLastNames, tableFormat);
        selectionModel = new DefaultEventSelectionModel(filteredLastNames);

        table.setModel(model);
        table.setSelectionModel(selectionModel);
    }

    class FirstNameTextFilterator implements TextFilterator {

        @Override
        public void getFilterStrings(List list, Person person) {
            list.add(person.getFirstName());
        }
    }

    class LastNameTextFilterator implements TextFilterator {

        @Override
        public void getFilterStrings(List list, Person person) {
            list.add(person.getLastName());
        }
    }

    public class Person {
        private String firstName;
        private String lastName;

        public Person(String firstName, String lastName) {
            this.firstName = firstName;
            this.lastName = lastName;
        }

        public String getFirstName() {
            return firstName;
        }

        public String getLastName() {
            return lastName;
        }
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new GlazedListsMultipleTextfields();
            }
        });
    }
}