943,812 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 8012
  • Java RSS
Aug 14th, 2007
0

Filter JTextField and Display in JTable

Expand Post »
hai , all
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)
  1. //DBAccess.java
  2. import ca.odell.glazedlists.BasicEventList;
  3. import ca.odell.glazedlists.EventList;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class DBAccess {
  11. private static Connection c;
  12. private static Statement selectStatement;
  13. static{
  14. try{
  15. Class.forName("com.mysql.jdbc.Driver");
  16. c = DriverManager.getConnection("jdbc:mysql://localhost/MyDB","root","");
  17. selectStatement = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  18. } catch(ClassNotFoundException e){
  19. e.printStackTrace();
  20. } catch(SQLException e){
  21. e.printStackTrace();
  22. }
  23. }
  24.  
  25. public static EventList<Customer> getCustomer ()throws SQLException{
  26. EventList<Customer> list = new BasicEventList<Customer>();
  27. ResultSet rs = selectStatement.executeQuery("select * from customers");
  28. while(rs.next()){
  29. list.add(getCustomer(rs));
  30. }
  31. return list;
  32. }
  33.  
  34.  
  35. public static Customer getCustomer(ResultSet rs)throws SQLException{
  36. Customer customer = new Customer();
  37. customer.setCode(rs.getString(1));
  38. customer.setName(rs.getString(2));
  39. return customer
  40. }
  41. }
  42.  
  43.  
  44. //Customer.java
  45. package newpackage;
  46. public class Customer {
  47. private String code, name;
  48. public Customer () {}
  49.  
  50. public String getCode() {
  51. return code;
  52. }
  53.  
  54. public void setCode(String code) {
  55. this.code = code;
  56. }
  57.  
  58. public String getName() {
  59. return name;
  60. }
  61.  
  62. public void setName(String name) {
  63. this.name = name;
  64. }
  65.  
  66. public String toString() {
  67. return getCode() + " " + getName();
  68. }
  69. }
  70.  
  71. //CustomerComparator.java
  72. package newpackage;
  73. import java.util.Comparator;
  74. public class CustomerComparator implements Comparator{
  75.  
  76. public CustomerComparator() {}
  77. public int compare(Object a, Object b) {
  78. Customer cusA = (Customer) a;
  79. Customer cusB = (Customer) b;
  80. if(a.toString().equals(b.toString()))
  81. return 0;
  82. return cusA.getCode().compareTo(cusB.getCode());
  83. }
  84. }
  85.  
  86. //CustomerFilter.java
  87. package newpackage;
  88. import ca.odell.glazedlists.TextFilterator;
  89. import java.util.List;
  90. import newpackage.Customer;
  91. public class CustomerFilter implements TextFilterator{
  92. public CustomerFilter() { }
  93. public void getFilterStrings(List baseList, Object element) {
  94. Customer customer = (Customer) element;
  95. baseList.add(customer.getCode());
  96. baseList.add(customer.getName());
  97. }
  98. }
  99.  
  100.  
  101. //CustomerTableFormat.java
  102. package newpackage;
  103. import ca.odell.glazedlists.gui.TableFormat;
  104. public class CustomerTableFormat implements TableFormat{
  105. public CustomerTableFormat() {}
  106. public int getColumnCount() {
  107. return 2;
  108. }
  109.  
  110. public String getColumnName(int column) {
  111. switch(column){
  112. case 0: return "Code";
  113. case 1: return "Name";
  114. default: return "";
  115. }
  116. }
  117.  
  118. public Object getColumnValue(Object baseObject,int column) {
  119. Customer customer = (Customer) baseObject;
  120. switch(column){
  121. case 0: return customer.getCode();
  122. case 1: return customer.getName();
  123. default: return "";
  124. }
  125. }
  126. }
  127.  
  128.  
  129. //CustomerTableFilter.java
  130. package newpackage;
  131. import ca.odell.glazedlists.EventList;
  132. import ca.odell.glazedlists.FilterList;
  133. import ca.odell.glazedlists.SortedList;
  134. import ca.odell.glazedlists.swing.EventTableModel;
  135. import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
  136. public class CustomerTableFilter extends javax.swing.JFrame {
  137. public CustomerTableFilter() {
  138. initComponents();
  139. try{
  140. EventList<Customer> list = DBAccess.getCustomer();
  141. SortedList<Customer> sortedList = new SortedList<Customer>(list, new CustomerComparator());
  142. FilterList filterList = new FilterList(sortedList, new TextComponentMatcherEditor(txtFilter,new CustomerFilter()));
  143. EventTableModel model = new EventTableModel(filterList, new CustomerTableFormat());
  144. tblCustomer.setModel(model);
  145. }catch (Exception e){e.printStackTrace();}
  146. }
  147.  
  148.  
  149. public static void main(String args[]) {
  150. java.awt.EventQueue.invokeLater(new Runnable() {
  151. public void run() {
  152. new CustomerTableFilter().setVisible(true);
  153. }
  154. });
  155. }
  156.  
  157. // Variables declaration - do not modify
  158. private javax.swing.JScrollPane jScrollPane1;
  159. private javax.swing.JTable tblCustomer;
  160. private javax.swing.JTextField txtFilter;
  161. // End of variables declaration
  162.  
  163. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
onsir is offline Offline
38 posts
since Apr 2007
Aug 14th, 2007
-1

Re: Filter JTextField and Display in JTable

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.
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006
Aug 15th, 2007
0

Re: Filter JTextField and Display in JTable

i have tried, but still error or data can't display in jtable


Java Syntax (Toggle Plain Text)
  1. // NewJFrame.java
  2. package newpackage;
  3.  
  4. import ca.odell.glazedlists.BasicEventList;
  5. import ca.odell.glazedlists.EventList;
  6. import ca.odell.glazedlists.FilterList;
  7. import ca.odell.glazedlists.SortedList;
  8. import ca.odell.glazedlists.TextFilterator;
  9. import ca.odell.glazedlists.gui.TableFormat;
  10. import ca.odell.glazedlists.swing.EventTableModel;
  11. import ca.odell.glazedlists.swing.TextComponentMatcherEditor;
  12. import java.sql.Connection;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.util.Comparator;
  17. import java.util.List;
  18.  
  19. public class NewJFrame extends javax.swing.JFrame {
  20. private static Connection c;
  21. private static Statement selectStatement;
  22. public NewJFrame() {
  23. initComponents();
  24.  
  25. try{
  26. EventList<Customer> list =getCustomer();
  27. SortedList<Customer> sortedList = new SortedList<Customer>(list, new CustomerComparator());
  28. FilterList filterList = new FilterList(sortedList, new TextComponentMatcherEditor(txtFilter,new CustomerFilter()));
  29. EventTableModel model = new EventTableModel(filterList, new CustomerTableFormat());
  30. tblCustomer.setModel(model);
  31. }catch (Exception e){e.printStackTrace();}
  32.  
  33. }
  34.  
  35. public class Customer {
  36. private String code, name;
  37.  
  38. public String getCode() {
  39. return code;
  40. }
  41.  
  42. public void setCode(String code) {
  43. this.code = code;
  44. }
  45.  
  46. public String getName() {
  47. return name;
  48. }
  49.  
  50. public void setName(String name) {
  51. this.name = name;
  52. }
  53.  
  54.  
  55. public String toString() {
  56. return getCode() + " " + getName();
  57. }
  58. }
  59.  
  60.  
  61. class CustomerComparator implements Comparator{
  62. public int compare(Object a, Object b) {
  63. Customer cusA = (Customer) a;
  64. Customer cusB = (Customer) b;
  65.  
  66. if(a.toString().equals(b.toString()))
  67. return 0;
  68. return cusA.getCode().compareTo(cusB.getCode());
  69. }
  70.  
  71. }
  72.  
  73.  
  74. class CustomerFilter implements TextFilterator{
  75. public void getFilterStrings(List baseList, Object element) {
  76. Customer customer = (Customer) element;
  77. baseList.add(customer.getCode());
  78. baseList.add(customer.getName());
  79. }
  80. }
  81.  
  82.  
  83. public class CustomerTableFormat implements TableFormat{
  84. public int getColumnCount() {
  85. return 2;
  86. }
  87.  
  88. public String getColumnName(int column) {
  89. switch(column){
  90. case 0: return "Code";
  91. case 1: return "Name";
  92. default: return "";
  93. }
  94. }
  95.  
  96. public Object getColumnValue(Object baseObject,int column) {
  97. Customer customer = (Customer) baseObject;
  98. switch(column){
  99. case 0: return customer.getCode();
  100. case 1: return customer.getName();
  101. default: return "";
  102. }
  103. }
  104. }
  105.  
  106. public static EventList<Customer> getCustomer()throws SQLException{
  107. EventList<Customer> list = new BasicEventList<Customer>();
  108. ResultSet rs = DBAccess.selectStatement.executeQuery("select * from customer");
  109. while(rs.next()){
  110. list.add(getCustomer(rs));
  111. }
  112. return list;
  113. }
  114.  
  115.  
  116. public static Customer getCustomer(ResultSet rs)throws SQLException{
  117. Customer customer = null; // data can't show in jtable
  118. //Customer customer = new Customer(); //err non-static variable this cannot be referenced from a static context
  119. customer.setCode(rs.getString(1));
  120. customer.setName(rs.getString(2));
  121. return customer;
  122. }
  123.  
  124.  
  125. /** This method is called from within the constructor to
  126.   * initialize the form.
  127.   * WARNING: Do NOT modify this code. The content of this method is
  128.   * always regenerated by the Form Editor.
  129.   */
  130. // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
  131. private void initComponents() {
  132. jScrollPane1 = new javax.swing.JScrollPane();
  133. tblCustomer = new javax.swing.JTable();
  134. txtFilter = new javax.swing.JTextField();
  135.  
  136. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  137. tblCustomer.setModel(new javax.swing.table.DefaultTableModel(
  138. new Object [][] {
  139. {null, null, null, null},
  140. {null, null, null, null},
  141. {null, null, null, null},
  142. {null, null, null, null}
  143. },
  144. new String [] {
  145. "Title 1", "Title 2", "Title 3", "Title 4"
  146. }
  147. ));
  148. jScrollPane1.setViewportView(tblCustomer);
  149.  
  150. org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
  151. getContentPane().setLayout(layout);
  152. layout.setHorizontalGroup(
  153. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  154. .add(layout.createSequentialGroup()
  155. .addContainerGap()
  156. .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  157. .add(txtFilter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 188, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  158. .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 375, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  159. .addContainerGap(15, Short.MAX_VALUE))
  160. );
  161. layout.setVerticalGroup(
  162. layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
  163. .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
  164. .add(txtFilter, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
  165. .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  166. .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 275, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
  167. );
  168. pack();
  169. }// </editor-fold>
  170.  
  171. /**
  172.   * @param args the command line arguments
  173.   */
  174. public static void main(String args[]) {
  175. java.awt.EventQueue.invokeLater(new Runnable() {
  176. public void run() {
  177. new NewJFrame().setVisible(true);
  178. }
  179. });
  180. }
  181.  
  182.  
  183. // Variables declaration - do not modify
  184. private javax.swing.JScrollPane jScrollPane1;
  185. private javax.swing.JTable tblCustomer;
  186. private javax.swing.JTextField txtFilter;
  187. // End of variables declaration
  188.  
  189. }
  190.  
  191.  
  192. //DBAccess.java
  193.  
  194. package newpackage;
  195.  
  196. import ca.odell.glazedlists.BasicEventList;
  197. import ca.odell.glazedlists.EventList;
  198. import java.sql.Connection;
  199. import java.sql.DriverManager;
  200. import java.sql.ResultSet;
  201. import java.sql.SQLException;
  202. import java.sql.Statement;
  203. public class DBAccess {
  204. public static Connection c;
  205. public static Statement selectStatement;
  206.  
  207. static{
  208. try{
  209. Class.forName("com.mysql.jdbc.Driver");
  210. c = DriverManager.getConnection("jdbc:mysql://localhost/mydb","root","");
  211. selectStatement = c.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
  212. } catch(ClassNotFoundException e){
  213. e.printStackTrace();
  214. } catch(SQLException e){
  215. e.printStackTrace();
  216. }
  217. }
  218.  
  219. }
Reputation Points: 10
Solved Threads: 1
Light Poster
onsir is offline Offline
38 posts
since Apr 2007
Aug 15th, 2007
0

Re: Filter JTextField and Display in JTable

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:
Java Syntax (Toggle Plain Text)
  1. public static Customer getCustomer(ResultSet rs)throws SQLException{
  2. Customer customer = null; // data can't show in jtable
  3. //Customer customer = new Customer(); //err non-static variable this cannot be referenced from a static context
  4. customer.setCode(rs.getString(1));
  5. customer.setName(rs.getString(2));
  6. return customer;
  7. }
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?
Reputation Points: 46
Solved Threads: 11
Junior Poster
orko is offline Offline
164 posts
since Apr 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: class, interface, or enum expected
Next Thread in Java Forum Timeline: accessing info created in one class, in another class





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC