Tablesorter和过滤表中的数据 [英] Tablesorter and filtering data in a table

查看:138
本文介绍了Tablesorter和过滤表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,当用户在文本字段中键入时,过滤表中的数据



here is my code to filter data in a table when the user type in the textfield

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



但它甚至没有显示表格。



ps:我创建了MyTableSorter,它从DefaultTableSorter类扩展而来


But it''s not even showing the table.

ps:MyTableSorter i created that that extends from the DefaultTableSorter class

推荐答案

首先请将你的类重命名为不同的东西 - Frame AWT 占用,这可能会毁掉你使用该名称的一天。



你应该在这里阅读:如何在Swing中使用表 [ ^ ] @ Oracle教程。



你有一个数据绑定 - 或者至少尝试制作一个。请检查您是否有LabelProvider以及您从DB数据设置的模型是否有效。



让您的TableSorter也以通用方式工作 - 您将其添加到表并在您创建的自定义表分类器中定义排序规则。

就是这样,不需要像在filterTables()中那样设置新模型;
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();


这篇关于Tablesorter和过滤表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆