简体   繁体   中英

Searching JTable for data inputted via a JTextField data

I have a JTable populated with data from a database. I have added a search function that highlights a cell when the data is typed into a JTextField. But at the minute you have to type in the exact word or number, what I want to do is be able to search for just a few letters or numbers, and the JTable will show the results containing those letters or numbers.

I have tried to use:

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

But it does seem to affect the JTable.

So I have used this piece of code:

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

Which works using the data typed into the JTextField, but like I mentioned earlier you have to type in the exact name or number. Which is not how I want it to work.

Any help is appreciated.

Your test

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

causes the behaviour you describe. equals will only work for an exact match.

You could try something a little more sophisticated like

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

Personnally, I would perform this test in your renderer so that all matching cells are highlighted (you will still need to perform the scrollRect() but not in the renderer):

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

And evertyime the actionPerformed is called, I would invoke table.repaint();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM