在JTable中搜索通过JTextField数据输入的数据

时间:2013-01-02 22:29:43

标签: java swing jtable jtextfield

我有一个JTable,其中填充了数据库中的数据。我添加了一个搜索功能,在数据输入JTextField时突出显示一个单元格。但是,当你必须输入确切的单词或数字时,我想要做的是只能搜索几个字母或数字,JTable将显示包含这些字母或数字的结果。

我试过用:

button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            String text = jTextField1.getText();
            if (text.length() == 0) {
                sorter.setRowFilter(null);
            } else {
                sorter.setRowFilter(RowFilter.regexFilter(text));
            }
        }
    });

但它似乎确实影响了JTable。

所以我使用了这段代码:

private class HighlightRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        // everything as usual
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        // added behavior
        if(row == table.getSelectedRow()) {

            // this will customize that kind of border that will be use to highlight a row
            setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
        }

        return this;
    }
}

private void searchResultsActionPerformed(ActionEvent evt) {
    String value = jTextField1.getText();

    for (int row = 0; row <= table.getRowCount() - 1; row++) {

        for (int col = 0; col <= table.getColumnCount() - 1; col++) {

            if (value.equals(table.getValueAt(row, col))) {

                // this will automatically set the view of the scroll in the location of the value
                table.scrollRectToVisible(table.getCellRect(row, 0, true));

                // this will automatically set the focus of the searched/selected row/value
                table.setRowSelectionInterval(row, row);

                for (int i = 0; i <= table.getColumnCount() - 1; i++) {

                    table.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer());
                }
            }
        }
    }
}

使用键入JTextField的数据可以正常工作,但就像我之前提到的那样,您必须输入确切的名称或编号。这不是我希望它工作的方式。

感谢任何帮助。

1 个答案:

答案 0 :(得分:4)

你的考试

value.equals(table.getValueAt(row, col))

会导致您描述的行为。 equals仅适用于完全匹配。

你可以尝试一些更复杂的东西,比如

Object value = table.getValueAt(row, col);
if (value!=null && value.toString().matches(".*"+Pattern.quote(text)+".*"))
   ...

在个人情况下,我会在您的渲染器中执行此测试,以便突出显示所有匹配的单元格(您仍然需要执行scrollRect()但不需要执行渲染器):

private class HighlightRenderer extends DefaultTableCellRenderer {

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        // everything as usual
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        // added behavior
        if (value!=null && value.toString().matches(".*"+Pattern.quote(text)+".*")) {

            // this will customize that kind of border that will be use to highlight a row
            setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.BLACK));
        } else
            setBorder(BorderFactory.createEmptyBorder(1, 1, 1, 1);

        return this;
    }
}

evertyime actionPerformed被调用,我会调用table.repaint();