Java – Quickly search for bulk data in JTable via JTextField

Quickly search for bulk data in JTable via JTextField… here is a solution to the problem.

Quickly search for bulk data in JTable via JTextField

I created an application that searches for data listed in JTable while entering the required data in JTextField.

The app works with a small amount of data, but in the case of a large amount of data, the search seems to be a bit delayed.

Can anyone tell me some solutions/suggestions/other ways to fix this.

My code is as follows.

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
/**
 *
 * @author user55
 */
public class searching extends javax.swing.JFrame {
final TableRowSorter<TableModel> sorter;
    /**
     * Creates new form searching
     */
    public searching() {
        initComponents();
        int a=1,b=1000000,c=2000000;

Object rows[][]=new Object[1000000][5];
  for(int i=0; i<1000000; i++)
  {
    rows[i][0]=a++;
    rows[i][1]=b++;
    rows[i][2]=c++;

}
    String columns[] = { "Symbol", "Name", "Price" };
    TableModel model = new DefaultTableModel(rows, columns) {
      public Class getColumnClass(int column) {
        Class returnValue;
        if ((column >= 0) && (column < getColumnCount())) {
          returnValue = getValueAt(0, column).getClass();
        } else {
          returnValue = Object.class;
        }
        return returnValue;
      }
    };

table1.setModel(model);
    sorter = new TableRowSorter<TableModel>(model);
    table1.setRowSorter(sorter);
    }

/**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
     <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

jScrollPane1 = new javax.swing.JScrollPane();
        filterText = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

table1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3"
            }
        ));
        jScrollPane1.setViewportView(table1);

filterText.addInputMethodListener(new java.awt.event.InputMethodListener() {
            public void caretPositionChanged(java.awt.event.InputMethodEvent evt) {
            }
            public void inputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
                filterTextInputMethodTextChanged(evt);
            }
        });
        filterText.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                filterTextKeyTyped(evt);
            }
        });

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 472, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                            .addComponent(filterText)
                            .addGap(263, 263, 263))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(19, 19, 19)
                    .addComponent(filterText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(18, 18, 18)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(22, Short.MAX_VALUE)))
        );

pack();
    }// </editor-fold>

private void filterTextInputMethodTextChanged(java.awt.event.InputMethodEvent evt) {
        String text = filterText.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(RowFilter.regexFilter(text));
        }
    }

private void filterTextKeyTyped(java.awt.event.KeyEvent evt) {
        String text = filterText.getText();
        if (text.length() == 0) {
            sorter.setRowFilter(null);
        } else {
            sorter.setRowFilter(RowFilter.regexFilter(text));
        }
    }

/**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        <editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(searching.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(searching.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(searching.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(searching.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        </editor-fold>

/*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {
                new searching().setVisible(true);
            }
        });
    }
     Variables declaration - do not modify
    private javax.swing.JTextField filterText;
    private javax.swing.JScrollPane jScrollPane1;
    private static final javax.swing.JTable table1 = new javax.swing.JTable();
     End of variables declaration
}

Solution

  • RowFilter searches throughout the XxxTableModel, for all rows, for each column, for large models you must change if (text.length() > 2) { , you can still delay the first search, but reduce to searching for a single character or a combination of two characters, It’s also a tough job for memory and processors

  • Apply filtering to only one of the columns

  • It’s not a good idea to display a bunch of data that can never be read, it’s the job of Sql interpeter

  • getText comes from Document (JTextComponents‘ model) as a parameter to RowFilter

Related Problems and Solutions