vendredi 1 janvier 2016

How to dynamically filter jTable from textfield in netbeans from SQLite?

I'm very new to this to please excuse my lack of knowledge.

I am making a Document manager database with SQLite and I am trying to get my jTextField KeyReleased search to update dynamically and filter my jTable as I type to show all matching results.

I am told that the jTable already has functionality to do filtering without additional queries but all I was able to find was the autoCreateRowSorter which is not what I'm trying to do.

Here is my initial code for the table connection and my update table method.

import com.sun.glass.events.KeyEvent;
import java.awt.HeadlessException;
import java.io.FileNotFoundException;
import java.sql.*;
import javax.swing.*;
import net.proteanit.sql.DbUtils;
import java.text.MessageFormat;
import java.io.FileOutputStream;
import java.util.Date;

public class Employee_info extends javax.swing.JFrame {
Connection conn=null;
ResultSet rs =null;
PreparedStatement pst=null;

/**
 * Creates new form Employee_info
 */
public Employee_info() {
    initComponents();
    conn=javaconnect.ConnecrDb();
    Update_table();
    Fillcombo();
}

private void Update_table(){

try{
String sql ="select * from Documents";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
table_documents.setModel(DbUtils.resultSetToTableModel(rs));
table_documents.getColumnModel().getColumn(0).setPreferredWidth(50);
table_documents.getColumnModel().getColumn(1).setPreferredWidth(1);
table_documents.getColumnModel().getColumn(2).setPreferredWidth(300);
table_documents.getColumnModel().getColumn(3).setPreferredWidth(1);
table_documents.getColumnModel().getColumn(4).setPreferredWidth(150);
}
catch (Exception e){
    JOptionPane.showMessageDialog(null, e);

} finally {   
try{
 rs.close();
 pst.close();
 //conn.close();
}       
catch (Exception e) {
                }  
}
}

here is my jTextfild event..(obvious I know).

private void txt_filterKeyReleased(java.awt.event.KeyEvent evt) {                                       
    // TODO add your handling code here:

}

I understand that my code is crude and basic so apologies.

I just don't know where to start with this so all and any help would be greatly appreciated.

Ryan

Aucun commentaire:

Enregistrer un commentaire