Note that there are some explanatory texts on larger screens.

plurals
  1. POSearching JTable for data inputted via a JTextField data
    primarykey
    data
    text
    <p>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.</p> <p>I have tried to use:</p> <pre><code>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)); } } }); </code></pre> <p>But it does seem to affect the JTable.</p> <p>So I have used this piece of code:</p> <pre><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 &lt;= table.getRowCount() - 1; row++) { for (int col = 0; col &lt;= 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 &lt;= table.getColumnCount() - 1; i++) { table.getColumnModel().getColumn(i).setCellRenderer(new HighlightRenderer()); } } } } } </code></pre> <p>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.</p> <p>Any help is appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload