hai , all
This coding about filter data using jTextFiled and display in jtable, so help me to combine these class be one class.
thanks
//DBAccess.java
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DBAccess {
private static Connection c;
private static Statement selectStatement;
static{
try{
Class.forName("com.mysql.jdbc.Driver");
c = DriverManager.getConnection("jdbc:mysql://localhost/MyDB","root","");
selectStatement = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
} catch(ClassNotFoundException e){
e.printStackTrace();
} catch(SQLException e){
e.printStackTrace();
}
}
public static EventList<Customer> getCustomer ()throws SQLException{
EventList<Customer> list = new BasicEventList<Customer>();
ResultSet rs = selectStatement.executeQuery("select * from customers");
while(rs.next()){
list.add(getCustomer(rs));
}
return list;
}
public static Customer getCustomer(ResultSet rs)throws SQLException{
Customer customer = new Customer();
customer.setCode(rs.getString(1));
customer.setName(rs.getString(2));
return customer
}
}
//Customer.java
package newpackage;
public class Customer {
private String code, name;
public Customer () {}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return getCode() + " " + getName();
}
}
//CustomerComparator.java
package newpackage;
import java.util.Comparator;
public class CustomerComparator implements Comparator{
public CustomerComparator() {}
public int compare(Object a, Object b) {
Customer cusA = (Customer) a;
Customer cusB = (Customer) b;
if(a.toString().equals(b.toString()))
return 0;
return cusA.getCode().compareTo(cusB.getCode());
}
}
//CustomerFilter.java
package newpackage;
import ca.odell.glazedlists.TextFilterator;
import java.util.List;
import newpackage.Customer;
public class CustomerFilter implements TextFilterator{
public CustomerFilter() { }
public void getFilterStrings(List baseList, Object element) {
Customer customer = (Customer) element;
baseList.add(customer.getCode());
baseList.add(customer.getName());
}
}
//CustomerTableFormat.java
package newpackage;
import ca.odell.glazedlists.gui.TableFormat;
public class CustomerTableFormat implements TableFormat{
public CustomerTableFormat() {}
public int getColumnCount() {
return 2;
}
public String getColumnName(int column) {
switch(column){
case 0: return "Code";
case 1: return "Name";
default: return "";
}
}
public Object getColumnValue(Object baseObject,int column) {
Customer customer = (Customer) baseObject;
switch(column){
case 0: return customer.getCode();
case 1: return customer.getName();
default: return "";
}
}
}
//CustomerTableFilter.java
package newpackage;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
public class CustomerTableFilter extends javax.swing.JFrame {
public CustomerTableFilter() {
initComponents();
try{
EventList<Customer> list = DBAccess.getCustomer();
SortedList<Customer> sortedList = new SortedList<Customer>(list, new CustomerComparator());
FilterList filterList = new FilterList(sortedList, new TextComponentMatcherEditor(txtFilter,new CustomerFilter()));
EventTableModel model = new EventTableModel(filterList, new CustomerTableFormat());
tblCustomer.setModel(model);
}catch (Exception e){e.printStackTrace();}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new CustomerTableFilter().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTable tblCustomer;
private javax.swing.JTextField txtFilter;
// End of variables declaration
}