Reputation: 3
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.
Upvotes: 0
Views: 1766
Reputation: 324207
Start by reading the section from the Swing tutorial on Sorting and Filtering for a working example that does exactly what you want.
I suggest you bookmark the table of contents so you have a quick reference to basic Swing features.
Also method names should NOT start with an upper case character or contain an "_" in the name. Follow Java conventions which a used by the Java API.
Upvotes: 2