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                   
    
}

Recommended Answers

All 3 Replies

it is always good to use diff classes in java... datz the whole point of OO programming... i don't know y u r trying to put them together.
n wat classes do u want to combine? watz the approach u have made? show us some of ur work so that we can figure out where u r facing problem.

commented: Per the forum rules, please use proper English. +0

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(); 
        }   
   }

 }

seems like you have taken the code from some where else (a lot of things are inconsistent... i don't see how you are initiating the customer.)..... for programming you must need to know what your code is doing.
for example, in following code:

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;
    }

how you can access to the customer if you have not created any?
you need at least a constructor to make customer class work. wat are the errors you are getting?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.