有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java GlazedList单个列筛选

我目前有一个表,在各个列标题中有一个textfield,它根据每个列进行过滤。。我试着用上釉列表来代替,但我该怎么做呢?每列都应该有一个单独的文本字段,并根据所有文本字段进行过滤,这些文本字段根据单独的列进行过滤。 例如:列有“名字”和“姓氏”的表格。 该表将根据名字过滤器中的名字和姓氏过滤器中的姓氏筛选结果


共 (1) 个答案

  1. # 1 楼答案

    首先你需要弄清楚的是,GlazedList主要是关于处理列表的对象的,线索在名称中。许多人将其视为添加排序和;将功能过滤到表和列表,这肯定会引起麻烦

    因此,首先关注glazedList将为您提供一种称为EventList的特殊类型的列表结构,这是您的核心数据结构;从那里,您将获得用于转换、排序、筛选等的有用方法。然后,由于它非常慷慨,有易于使用的类将您的事件列表链接到JList或JTable

    一旦这些都连接好了,对列表的更改和转换将自动传播到链接到列表的任何Swing组件

    无论如何,这里有一个示例类,它将向您展示如何创建事件列表,然后通过Swing组件应用文本过滤器,然后将其链接到Swing表。(这不包括排序,也不包括如何获取所选项目,但文档非常优秀。)

    使用Java9和GlazedList 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();
                }
            });
        }
    }