i have tried, but still error or data can't display in jtable
// NewJFrame.java
package newpackage;
import ca.odell.glazedlists.BasicEventList;
import ca.odell.glazedlists.EventList;
import ca.odell.glazedlists.FilterList;
import ca.odell.glazedlists.SortedList;
import ca.odell.glazedlists.TextFilterator;
import ca.odell.glazedlists.gui.TableFormat;
import ca.odell.glazedlists.swing.EventTableModel;
import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Comparator;
import java.util.List;
public class NewJFrame extends javax.swing.JFrame {
private static Connection c;
private static Statement selectStatement;
public NewJFrame() {
initComponents();
try{
EventList<Customer> list =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 class Customer {
private String code, name;
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();
}
}
class CustomerComparator implements Comparator{
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());
}
}
class CustomerFilter implements TextFilterator{
public void getFilterStrings(List baseList, Object element) {
Customer customer = (Customer) element;
baseList.add(customer.getCode());
baseList.add(customer.getName());
}
}
public class CustomerTableFormat implements TableFormat{
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 "";
}
}
}
public static EventList<Customer> getCustomer()throws SQLException{
EventList<Customer> list = new BasicEventList<Customer>();
ResultSet rs = DBAccess.selectStatement.executeQuery("select * from customer");
while(rs.next()){
list.add(getCustomer(rs));
}
return list;
}
public static Customer getCustomer(ResultSet rs)throws SQLException{
Customer customer = null; // data can't show in jtable
//Customer customer = new Customer(); //err non-static variable this cannot be referenced from a static context
customer.setCode(rs.getString(1));
customer.setName(rs.getString(2));
return customer;
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">
private void initComponents() {
jScrollPane1 = new javax.swing.JScrollPane();
tblCustomer = new javax.swing.JTable();
txtFilter = new javax.swing.JTextField();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
tblCustomer.setModel(new javax.swing.table.DefaultTableModel(
new Object [][] {
{null, null, null, null},
{null, null, null, null},
{null, null, null, null},
{null, null, null, null}
},
new String [] {
"Title 1", "Title 2", "Title 3", "Title 4"
}
));
jScrollPane1.setViewportView(tblCustomer);
org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(layout.createSequentialGroup()
.addContainerGap()
.add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(txtFilter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
.addContainerGap(15, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
.add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
.add(txtFilter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
);
pack();
}// </editor-fold>
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new NewJFrame().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
}
//DBAccess.java
package newpackage;
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 {
public static Connection c;
public 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();
}
}
}