首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用RowSorter JTable搜索两个值

如何使用RowSorter JTable搜索两个值
EN

Stack Overflow用户
提问于 2017-10-07 12:13:19
回答 2查看 290关注 0票数 0

当我从ComboBox中选择类和节时,它将不得不在Jtable中搜索特定的类和节,并且只显示这些类和节?

EN

回答 2

Stack Overflow用户

发布于 2017-10-07 15:07:53

不要使用额外的组合框进行搜索,而是尝试使用SwingBits库来执行代码。

您可以找到jar文件Here

之后,在显示jTable中的所有数据后,将以下代码行添加到源代码中:

代码语言:javascript
复制
TableRowFilterSupport.forTable(jTable1).searchable(true).apply();

在此之后,您将能够分别使用鼠标的左击和右键在运行时对数据进行排序和过滤。

票数 0
EN

Stack Overflow用户

发布于 2017-10-07 22:33:07

这是自定义TableModel的示例

StudentTableModel.java

代码语言:javascript
复制
public class StudentTableModel extends AbstractTableModel implements TableModel {
    private List<Student> data;
    private List<Student> filtredList;

    private Predicate<Student> predicate = student -> true;

    public List<Student> getData() {
        return data;
    }

    public void setData(List<Student> data) {
        this.data = data;
        filter(predicate);
    }

    public void filter(Predicate<Student> predicate) {
        this.predicate = predicate;

        filtredList = data.stream()
                .filter(predicate)
                .collect(Collectors.toList());

        fireTableDataChanged();
    }

    @Override
    public String getColumnName(int column) {
        return "Title " + (column + 1);
    }

    @Override
    public int getColumnCount() {
        return 4;
    }

    @Override
    public int getRowCount() {
        if(filtredList == null) {
            return 0;
        }

        return filtredList.size();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        Student student = filtredList.get(rowIndex);
        switch (columnIndex) {
            case 0: return rowIndex + 1;
            case 1: return student.getName();
            case 2: return student.getClassName();
            case 3: return student.getSectionName();

            default: return "N/A";
        }
    }
}

MainFrame.java

代码语言:javascript
复制
public class MainFrame extends JFrame {

    private JTextField textField;
    private JComboBox<String> classComboBox, sectionComboBox;
    private JTable table;

    public MainFrame(String title) {
        super(title);
        creageGUI();
    }

    private void creageGUI() {
        List<Student> list = new ArrayList<>();
        list.add(new Student("Name 1", "Class B", "1"));
        list.add(new Student("Name 2", "Class A", "1"));
        list.add(new Student("Name 3", "Class B", "3"));
        list.add(new Student("Name 4", "Class A", "1"));
        list.add(new Student("Name 5", "Class A", "2"));
        list.add(new Student("Name 6", "Class C", "1"));
        list.add(new Student("Name 7", "Class A", "3"));
        list.add(new Student("Name 8", "Class B", "4"));
        list.add(new Student("Name 9", "Class A", "1"));
        list.add(new Student("Name 10", "Class C", "2"));
        list.add(new Student("Name 11", "Class B", "1"));
        list.add(new Student("Name 12", "Class C", "1"));

        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLayout(new BorderLayout(0, 5));
        setMinimumSize(new Dimension(600, 480));

        textField = new JTextField(20);
        textField.getDocument().addDocumentListener(new DocumentListener() {
            @Override
            public void insertUpdate(DocumentEvent e) {
                actionListener(null);
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                actionListener(null);
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                actionListener(null);
            }
        });

        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        searchPanel.add(new JLabel("Search"));
        searchPanel.add(textField);

        classComboBox = new JComboBox<>(new String[]{"Class A", "Class B", "Class C"});
        classComboBox.setSelectedIndex(-1);
        classComboBox.addActionListener(this::actionListener);

        sectionComboBox = new JComboBox<>(new String[] {"1", "2", "3", "4", "5"});
        sectionComboBox.setSelectedIndex(-1);
        sectionComboBox.addActionListener(this::actionListener);

        JPanel comboBoxPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 5, 0));
        comboBoxPanel.add(new JLabel("Class"));
        comboBoxPanel.add(classComboBox);
        comboBoxPanel.add(Box.createHorizontalStrut(20));
        comboBoxPanel.add(new JLabel("Section"));
        comboBoxPanel.add(sectionComboBox);

        JPanel headerPanel = new JPanel();
        headerPanel.setLayout(new BoxLayout(headerPanel, BoxLayout.PAGE_AXIS));
        headerPanel.add(searchPanel);
        headerPanel.add(Box.createVerticalStrut(5));
        headerPanel.add(comboBoxPanel);

        StudentTableModel model = new StudentTableModel();
        model.setData(list);

        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);

        JButton clearFilterButton = new JButton("Clear");
        clearFilterButton.addActionListener(event -> {
            textField.setText("");
            classComboBox.setSelectedIndex(-1);
            sectionComboBox.setSelectedIndex(-1);
        });

        JPanel bottomPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        bottomPanel.add(clearFilterButton);

        add(headerPanel, BorderLayout.PAGE_START);
        add(scrollPane, BorderLayout.CENTER);
        add(bottomPanel, BorderLayout.PAGE_END);
    }

    private void actionListener(ActionEvent event) {
        StudentTableModel model = (StudentTableModel) table.getModel();
        model.filter(student -> {
            String name = textField.getText();
            String className = (String) classComboBox.getSelectedItem();
            String sectionName = (String) sectionComboBox.getSelectedItem();

            if(!name.isEmpty() && !student.getName().contains(name)) {
                return false;
            }

            if(className != null && !student.getClassName().equals(className)) {
                return false;
            }

            if(sectionName != null && !student.getSectionName().equals(sectionName)) {
                return false;
            }

            return true;
        });
    }
}

Student.java

代码语言:javascript
复制
public class Student {
    private String name;
    private String className;
    private String sectionName;

    public Student() {
    }

    public Student(String name, String className, String sectionName) {
        this.name = name;
        this.className = className;
        this.sectionName = sectionName;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getSectionName() {
        return sectionName;
    }

    public void setSectionName(String sectionName) {
        this.sectionName = sectionName;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46616610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档