在java

时间:2015-07-22 11:13:48

标签: java swing jtable tablerowsorter

我在Java中有几种形式有JTables。我只需要创建一个公共类(' TableFilter ')并使用此类来过滤表格中的表格。在这里,我传递JTable,JTextField和要搜索的列号(如果我想搜索整个表,我将传递911作为列号)。

我的问题是当我搜索不在表格中的值时,有时它并不会告知值不存在,有时它会显示消息n次并清除文本框,有时它不是正在寻找。这是在JTable中搜索值的正确方法吗?有人可以帮我解决这个问题。提前谢谢。

NB:我的JTable包含HTML数据和(或)普通对象值。

import java.awt.EventQueue;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class TableFilter {

    TableRowSorter<TableModel> sorter;

    public TableFilter() {
        System.out.println("Filter Property Enabled");
    }

    public void filterJTable(JTable tab, JTextField filterText, int oncolumn) {// o,1,2 -> for corresponding columns and 911 for whole
        System.out.println("filtering for " + tab.getName());
        if (oncolumn == 911) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    tab.setRowSorter(new TableRowSorter<>((DefaultTableModel) tab.getModel()));
                    sorter = (TableRowSorter<TableModel>) tab.getRowSorter();
                    filterText.getDocument().addDocumentListener(new DocumentListener() {
                        @Override
                        public void insertUpdate(DocumentEvent e) {
                            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterText.getText().trim()));
                            if (sorter.getViewRowCount() == 0) {
                                reload(filterText);
                            }
                        }

                        @Override
                        public void removeUpdate(DocumentEvent e) {
                            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterText.getText().trim()));
                            if (sorter.getViewRowCount() == 0) {
                                reload(filterText);
                            }
                        }

                        @Override
                        public void changedUpdate(DocumentEvent e) {
                            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterText.getText().trim()));
                            if (sorter.getViewRowCount() == 0) {
                                reload(filterText);
                            }
                        }

                    });

                }
            });
        } else {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    tab.setRowSorter(new TableRowSorter<>((DefaultTableModel) tab.getModel()));
                    sorter = (TableRowSorter<TableModel>) tab.getRowSorter();
                    filterText.getDocument().addDocumentListener(new DocumentListener() {
                        @Override
                        public void insertUpdate(DocumentEvent e) {
                            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterText.getText().trim(), oncolumn));
                            if (sorter.getViewRowCount() == 0) {
                                reload(filterText);
                            }
                        }

                        @Override
                        public void removeUpdate(DocumentEvent e) {
                            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterText.getText().trim(), oncolumn));
                            if (sorter.getViewRowCount() == 0) {
                                reload(filterText);
                            }
                        }

                        @Override
                        public void changedUpdate(DocumentEvent e) {
                            sorter.setRowFilter(RowFilter.regexFilter("(?i)" + filterText.getText().trim(), oncolumn));
                            if (sorter.getViewRowCount() == 0) {
                                reload(filterText);
                            }
                        }
                    });
                }
            });
        }

    }

    private void reload(JTextField filterText) {
        JOptionPane.showMessageDialog(null, "No values found", "Error", JOptionPane.ERROR_MESSAGE);
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {                
                filterText.setText("");
            }
        });
    }
}

0 个答案:

没有答案