Click here to Skip to main content
15,877,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here is my code to filter data in a table when the user type in the textfield

Java
public class Frame extends javax.swing.JFrame {

    Connection conn = null;  
    
    ResultSet rs = null;
        
    PreparedStatement pst = null;
        
    public void updateTable(){
    
        String sql = "select * from employee";
        
        try{
        pst = conn.prepareStatement(sql);
        
        rs = pst.executeQuery();
        
        table.setModel(DbUtils.resultSetToTableModel(rs));
        }catch(Exception e){
        JOptionPane.showMessageDialog(rootPane, e);
        }
    }
   
    public void sortTable(){
        
        MyTableModel tableModel ;
        
        TableRowSorter<mytablemodel> sorter;
        
        tableModel = new MyTableModel();
        
        sorter = new TableRowSorter<mytablemodel>(tableModel);
        
        table.setModel(tableModel);
        
        table.setRowSorter(sorter);
        
        txtFilter.getDocument().addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                filterTable();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                filterTable();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
              filterTable();  
            }
        });
    }
    public void filterTable(){
         MyTableModel tableModel = new MyTableModel();
        
        TableRowSorter<mytablemodel> sorter = new TableRowSorter<mytablemodel>(tableModel);
            
      RowFilter<mytablemodel,> rf = null;
    //If current expression doesn't parse, don't update.
    try {
        rf = RowFilter.regexFilter(txtFilter.getText());
        
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);
      
    }
    private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
        // TODO add your handling code here:
        
        conn = connect.connectDb();
        
         updateTable();
        
         sortTable();
         
         filterTable();
    }                                 

     
     
    private void txtFilterKeyReleased(java.awt.event.KeyEvent evt) {                                      
      
    }                                     
  
  
    /**
     * @param args the command line arguments
     */
   
    public static void main(String args[]) {
       
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Frame().setVisible(true);
            }
        });
    }
} 

But it's not even showing the table.

ps:MyTableSorter i created that that extends from the DefaultTableSorter class
Posted
Updated 2-Jan-13 2:16am
v2

1 solution

First of all please rename your class to something different - Frame is taken by AWT, that can ruin your day to use that name.

You should read here: How to Use Tables in Swing[^] @ Oracle tutorials.

You have a data binding - or at least try to make one. Please check if you have a LabelProvider and if the Model you set from the DB data is valid.

Let your TableSorter also work that generic way - you add it to the table and define the sorting rules in the custom table sorter you create.
That's it, no need to set the model new like you seem to do in filterTables();
 
Share this answer
 
Comments
TorstenH. 2-Jan-13 8:33am    
That was way to much answers - my machine told me that it did not add the answer and I did so again and again.
One should be enough - please make your way along the tutorial and see where you come out.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900