swing - Java Resultset and AbstractTableModel to update JTable -


i'm beginner, have jtable want fill resultset should change everytime user enters search keyword in textfield , click search button. searched week , still don't know how use abstrattablemodel resultset show , refresh jtable after every search button click.

here entire code :

view.catalogueswing.java

public class catalogueswing extends jframe{    jlabel jlabelmc = new jlabel("key word");   jtextfield jtextfieldmc = new jtextfield(20); // textfield contains search keyword  jbutton jbuttonsearch = new jbutton("search");  cataloguebusiness ca; // business class contains "searchbykeyword method"  jtable table;  jpanel pcenter;  //---   public catalogueswing() {      ca = new cataloguebusiness();       jpanel pnorth = new jpanel();      pnorth.setlayout(new flowlayout());       pnorth.add(jlabelmc);      pnorth.add(jtextfieldmc);      pnorth.add(jbuttonsearch);       pcenter = new jpanel();      pcenter.setlayout(new borderlayout());       pcenter.add(table);       this.setlayout(new borderlayout());      this.add(pcenter, borderlayout.center);      this.setsize(900, 500);      pcenter.add(new jscrollpane(table));      this.add(pnorth, borderlayout.north);       this.setdefaultcloseoperation(exit_on_close);      this.setvisible(true);       jbuttonsearch.addactionlistener(new actionlistener() {              public void actionperformed(actionevent e)             {                 string kw = jtextfieldmc.gettext();                 table = new jtable(ca.searchbykeyword(kw)); // searchbykeyword(string kw) method in cataloguebusiness class in package.                 pcenter.add(table);                  system.out.println("you clicked button");             }         });    } 

here business class :

business.cataloguebusiness.java :

@override     public list<product> searchbykeyword(string kw) {         list<product> listproducts = new arraylist<product>();      try {         class.forname("com.mysql.jdbc.driver");         connection conn = drivermanager.getconnection("jdbc:mysql://localhost:3306/db_tpjdbc","root","password");          preparedstatement ps = conn.preparestatement("select * products name_prod ?");         ps.setstring(1, "%"+kw+"%");         resultset rs = ps.executequery();          while (rs.next()) {             productp = new produit();             p.setidproduct(rs.getint("id_prod"));             p.setnomproduct(rs.getstring("name_prod"));             p.setprice(rs.getdouble("price"));             p.setquantite(rs.getint("quantity"));              listproducts.add(p);             }           //ps.close();         //conn.close();      } catch (exception e) {         e.printstacktrace();     }      return listproducts; } 

and here product class :

business.product.java :

import java.io.serializable;  public class product implements serializable {      private int idproduct;     private string nomproduct;     private double price;     private int quantity;       public produit(string nomproduct, double price, int quantity) {         super();         this.nomproduct = nomproduct;         this.price = price;         this.quantity = quantity;     }     public product() {         super();     }     // getters & setter ...     // ... 

and absracttablemodel

business.productmodel.java

public class produitmodel extends abstracttablemodel{      @override     public int getcolumncount() {     //.... } @override public int getrowcount() {     //... } @override public object getvalueat(int rowindex, int columnindex) {     //... } @override public string getcolumnname(int column) { //... } 

i stopped here, don't know how use abstracttablemodel in case, searched lot , still have no idea how use resultset keep updating jtable everytime click "search button" keyword in search textfield.

thank in advance.

start having @ how use tables more details.

the tablemodel provides number of methods required jtable make decisions how best display data.

  • getrowcount, tells table how many rows has display
  • getcolumncount, tells table how many columns has display
  • getcolumnname, tells table name specific column has, displayed table header
  • getcolumnclass, used table make decisions cell renderer/editor should use in case custom renderer not supplied column
  • getvalueat, returns value given cell (row/column)
  • setvalueat, requests model update value given cell
  • iscelleditable, determines if given cell can edited or not

so, armed basic information, can generate simple tablemodel wraps list of products, example...

public static class producttablemodel extends abstracttablemodel {      protected static final string[] column_names = {"id", "name", "price", "quantity"};     protected static final class[] column_types = {integer.class, string.class, double.class, integer.class};     private list<product> products;      public producttablemodel(list<product> products) {         this.products = new arraylist<>(products);     }      @override     public int getrowcount() {         return products.size();     }      @override     public int getcolumncount() {         return column_names.length;     }      @override     public string getcolumnname(int column) {         return column_names[column];     }      @override     public class<?> getcolumnclass(int columnindex) {         return column_types[columnindex];     }      protected product getproductforrow(int row) {         return products.get(row);     }      @override     public object getvalueat(int rowindex, int columnindex) {         product product = getproductforrow(rowindex);         object value = null;         switch (columnindex) {             case 0:                 value = product.getidproduct();                 break;             case 1:                 value = product.getnomproduct();                 break;             case 2:                 value = product.getprice();                 break;             case 3:                 value = product.getquantity();                 break;         }         return value;     }  } 

now, pretty simple, non-editable example.

to use it, create new producttablemodel results searchbykeyword method , apply existing jtable...

jbuttonsearch.addactionlistener(new actionlistener() {     public void actionperformed(actionevent e)     {         string kw = jtextfieldmc.gettext();         table.setmodel(new producttablemodel(ca.searchbykeyword(kw)));     } });   

the table api third complex api in swing library (with underlying text component , jtree api more complex), bar far 1 of common things do. if can head around it, find rest of api relatively simple (including coming grips tree api ;))


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -