2

我有一个从数据库中填充数据的 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 中的数据可以工作,但就像我之前提到的,您必须输入确切的名称或编号。这不是我想要的工作方式。

任何帮助表示赞赏。

4

1 回答 1

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;
    }
}

并且每次actionPerformed调用时,我都会调用table.repaint();

于 2013-01-02T23:00:12.857 回答