| | |
Filter JTextField and Display in JTable
![]() |
•
•
Join Date: Apr 2007
Posts: 34
Reputation:
Solved Threads: 1
hai , all
This coding about filter data using jTextFiled and display in jtable, so help me to combine these class be one class.
thanks
This coding about filter data using jTextFiled and display in jtable, so help me to combine these class be one class.
thanks
Java Syntax (Toggle Plain Text)
//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 }
•
•
Join Date: Apr 2007
Posts: 34
Reputation:
Solved Threads: 1
i have tried, but still error or data can't display in jtable
Java Syntax (Toggle Plain Text)
// 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(); } } }
•
•
Join Date: Apr 2006
Posts: 164
Reputation:
Solved Threads: 10
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:
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?
for example, in following code:
Java Syntax (Toggle Plain Text)
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; }
you need at least a constructor to make customer class work. wat are the errors you are getting?
A Perfect World
![]() |
Similar Threads
- Assignment Help ASAP! (Java)
- FILTER QUERY results on a FORM? (Visual Basic 4 / 5 / 6)
- Itzi Bitzi Mitzi (Java)
- Multimap confusion; how does it work? (C)
- Program Output Display Tweak Help (Java)
- Making searching faster (Java)
- Help with a reservation program! GUI Messed XD (Java)
- How do you center the text in a JTextField?? (Java)
- JTables (Java)
- Memory buttons problem (Java)
Other Threads in the Java Forum
- Previous Thread: class, interface, or enum expected
- Next Thread: accessing info created in one class, in another class
| Thread Tools | Search this Thread |
911 actionlistener addball addressbook android api applet application array automation binary block bluetooth button character class client code component consumer css csv database desktop developmenthelp eclipse ee error fractal ftp game gameprogramming givemetehcodez graphics gui html ide image j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia jvm lego linked linux loan mac map method mobile netbeans notdisplaying number objects online oriented phone printf problem program programming project projects recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server service set singleton sms software sort sql swing system test textfields threads time title tree tutorial-sample ubuntu update windows working





