Update Table With Jtextfield Filtering

How to update my table with Jtextfield filtering?

I have a Jtable that show the all customers list that there are in database.  On this table there is textfield for filtering. When a user type a character in the textfield, table must be update and show all customers that their names started by that character same time. For example when I type 's' in textfield, table would synchronize show all customer list that start with s like 'sara' without pressing any key . I write this code , but it don't work and has DBexeption.  I want use of typedkey event.

// below code is in myFrame.java
private void jTextField1KeyTyped(java.awt.event.KeyEvent evt) {
String st=evt.getKeyText(2);
newCall(st);

}

// Variables declaration - do not modify
private javax.swing.JTable customerTable;
private javax.swing.JButton jButton1;
private javax.swing.JLabel jLabel1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextField jTextField1;
// End of variables declaration

private void newCall(String st){
newFill();
try {
List<Customer> customerList = CustomerDAO.getInstance().listCustomersWithName(st );
Vector customerData = new Vector();

for (Customer c : customerList) {
Vector v = c.getCustomerData();
customerData.add(v);
}

Vector column = new Vector();

column.add("pass");
column.add("phone");
column.add("assress");
column.add("name");
column.add("id");

DefaultTableModel model = new DefaultTableModel(customerData, column);

customerTable.setModel(model);

} catch (DBException ex) {
JOptionPane.showMessageDialog(null, "database exception .");
}

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
private void newFill(){
Vector customerData = new Vector();
customerData=null;
Vector column = new Vector();
column=null;
DefaultTableModel model = new DefaultTableModel(customerData, column);
customerTable.setModel(model);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~
private void fillCustomerList() {
try {
List<Customer> customerList = CustomerDAO.getInstance().listCustomers();

Vector customerData = new Vector();

for (Customer c : customerList) {
Vector v = c.getCustomerData();
customerData.add(v);
}

Vector column = new Vector();
column.add("pass");
column.add("phone");
column.add("assress");
column.add("name");
column.add("id");

DefaultTableModel model = new DefaultTableModel(customerData, column);

customerTable.setModel(model);

} catch (DBException ex) {
JOptionPane.showMessageDialog(null, "database exception .");
}
}

Solution:

Simple thing. 
Use the TableRowSorter!
 
Code: 

final TableRowSorter<TableModel> sorter = new TableRowSorter<TableModel>(model); 
 
And add a DocumentListener to your textfield - in insertUpdate and removeUpdate you call sorter.setRowFilter(RowFilter.regexFilter(yourtext field.getText(),indeces)); 

The insertUpdate method belongs to the listener so you have to add a documentlistener to your textfield !

Here is a simple example:

Code:

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.RowFilter;
import javax.swing.SwingUtilities;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

public class FilterSnippet {
 
 private final TableRowSorter<TableModel> sorter;
 private JTextField field;

 public FilterSnippet(){
  JFrame frame = new JFrame();
  JPanel panel = new JPanel(new BorderLayout());
  field = new JTextField(10);
  field.getDocument().addDocumentListener(new DocumentListener() {  // <---
   @Override
   public void removeUpdate(DocumentEvent e) {
    filter();
   }
   
   @Override
   public void insertUpdate(DocumentEvent e) {
    filter();
   }
   
   @Override
   public void changedUpdate(DocumentEvent e) {}
  });
  DefaultTableModel model = new DefaultTableModel(new String[][]{{"Sarah","foo"}, {"Abc","bar"}, {"def","foobar"}}, new String[]{"Name","Nickname"});
  JTable table = new JTable(model);
  sorter = new TableRowSorter<TableModel>(model);  // <-- 
  table.setRowSorter(sorter);  // <-- 
  panel.add(field, BorderLayout.PAGE_START);
  panel.add(new JScrollPane(table),BorderLayout.CENTER);
  frame.add(panel);
  frame.pack();
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setLocationRelativeTo(null);
  frame.setVisible(true);
 }

 protected void filter() {
  sorter.setRowFilter(RowFilter.regexFilter("^(?i)"+field.getText(),0));  // <--
 }

 public static void main(String[] args) {
  SwingUtilities.invokeLater(new Runnable() {
   public void run() {
    new FilterSnippet();
   }
  });
 }

 

Note:
The important rows are marked. These rows(similar) you have to put into your code.

Do you have a Java Problem?
Ask It in The Java Forum

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.