How to continuously fire filtering the table using keyRelease?

65 Views Asked by At

I have module where I need to populate all the data in the JTable. Beside of having table user can able filter the data in table via searching on the textbox. I already setup the table and create function to populate the data to the table.

Problem: Every time I type on the textbox the data on the table removed itself.

Goal: User can filter the data whatever they type on the textbox.

Here is example that I created already:

First Output

Here is what happen when I filter:

filtered

Here is the code:

Populate function:

public void populatedoctor() {
    try {
        Class.forName("com.mysql.cj.jdbc.Driver");
        
        Connection con;
        try {
            
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/health_check?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC","root","");
            
            String query = "SELECT * FROM users WHERE role = '3' ";
            
            Statement sta = con.createStatement();
            ResultSet rs = sta.executeQuery(query);
            
            DefaultTableModel model = new DefaultTableModel();
            model.addColumn("First Name");
            model.addColumn("Last Name");
            model.addColumn("Phone Number");
            model.addColumn("Email");
            
            while(rs.next()) {
                model.addRow(new Object[] {
                        rs.getString("first_name"),
                        rs.getString("last_name"),
                        rs.getString("phone_number"),
                        rs.getString("email"),
                });
            }
            
            rs.close();
            sta.close();
            con.close();
            
            table.setModel(model);
            table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
            table.getColumnModel().getColumn(0).setPreferredWidth(200);
            table.getColumnModel().getColumn(1).setPreferredWidth(200);
            table.getColumnModel().getColumn(2).setPreferredWidth(200);
            table.getColumnModel().getColumn(3).setPreferredWidth(200);
            
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

Window Listener:

addWindowListener(new WindowAdapter() {
        @Override
        public void windowOpened(WindowEvent e) {
            populatedoctor();
        }
    });

TextBox:

textField = new JTextField();
    textField.getDocument().addDocumentListener(new DocumentListener() {

        @Override
            public void changedUpdate(DocumentEvent e){
                
            String search_txt = textField.getText().toLowerCase();
            
            TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
            
            table.setRowSorter(sorter);
            sorter.setRowFilter(RowFilter.regexFilter(search_txt));
        }

        @Override
        public void insertUpdate(DocumentEvent e) {
            String search_txt = textField.getText().toLowerCase();
            
            TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
            
            table.setRowSorter(sorter);
            sorter.setRowFilter(RowFilter.regexFilter(search_txt));
        }

        @Override
        public void removeUpdate(DocumentEvent e) {
            // TODO Auto-generated method stub
            String search_txt = textField.getText().toLowerCase();
            
            TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model);
            
            table.setRowSorter(sorter);
            sorter.setRowFilter(RowFilter.regexFilter(search_txt));
        }
    });  
1

There are 1 best solutions below

0
Adeeb Mark On

you can use your filter and the code on the KeyEvent !

String text = textField.getText();

sorter = new TableRowSorter<>(((model)));


    if (text.trim().length() == 0) {
        
        sorter.setRowFilter(null);
        
    } else {
        
        sorter.setRowFilter(RowFilter.regexFilter("(?i)" + text));
        
    }

    table.setRowSorter(sorter);