java - populating JTable using values from another method -


i using eclipse & swing create project. trying populate jtable. i've fetched values database in method display_userinfo_in_jtable().

but not know how use method populate jtable. think method should have return type this, not know how can it?

package view;  import controller.*; import java.awt.eventqueue; import java.sql.connection; import java.sql.preparedstatement; import java.sql.resultset; import java.util.arraylist;  import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.jscrollpane; import javax.swing.jtable; import javax.swing.table.defaulttablemodel;  public class tableview {      private jframe frame;     private jtable table;      /**      * launch application.      */     public static void main(string[] args) {         eventqueue.invokelater(new runnable() {             public void run() {                 try {                     tableview window = new tableview();                     window.frame.setvisible(true);                 } catch (exception e) {                     e.printstacktrace();                 }             }         });     }      private arraylist<userinfo> userinfolist()     {         try         {             userinfo userinfo;             arraylist<userinfo> userinfolist = new arraylist<userinfo>();             connection sqlcon = db_con.getsqlconnection();             preparedstatement ps = sqlcon.preparestatement("select id,name,username,contact,gender temp");              resultset rs = ps.executequery();             while(rs.next())             {                 userinfo = new userinfo(                                         rs.getint("id"),                                         rs.getstring("name"),                                         rs.getstring("username"),                                         rs.getstring("contact"),                                         rs.getstring("gender")                                         );                 userinfolist.add(userinfo);             }             return userinfolist;         }         catch(exception ex)         {             system.out.println(ex.tostring());             return null;         }            }      public void display_userinfo_in_jtable()     {         try         {             arraylist<userinfo> list = userinfolist();             defaulttablemodel dtm = new defaulttablemodel();             object row[] = new object[5];              for(int = 0 ; < list.size(); i++)             {                 row[0] = list.get(i).getid();                 row[1] = list.get(i).getname();                 row[2] = list.get(i).getusername();                 row[3] = list.get(i).getcontact();                 row[4] = list.get(i).getgender();                  dtm.addrow(row);             }             table.setmodel(dtm);         }         catch(exception ex)         {             system.out.println(ex.tostring());         }     }      /**      * create application.      */     public tableview() {         initialize();         display_userinfo_in_jtable();      }      /**      * initialize contents of frame.      */     private void initialize() {         frame = new jframe();         frame.setbounds(100, 100, 598, 402);         frame.setdefaultcloseoperation(jframe.exit_on_close);         frame.getcontentpane().setlayout(null);          jpanel panel = new jpanel();         panel.setbounds(10, 11, 562, 341);         frame.getcontentpane().add(panel);         panel.setlayout(null);          table = new jtable();         //table.setbounds(49, 163, 92, -97);         jscrollpane jsp = new jscrollpane(table);         panel.add(table);     } } 

i've added table.setmodel(dtm); in method display_userinfo_in_jtable(), still can not result.

there many problems code :

  1. coding , naming conventions
  2. the displayuserinfoinjtable() never gets called (or atleast haven't posted code)
  3. the table model isn't supposed coded way
  4. there no column identifiers, rows
  5. you have used absolute layout (layout set null) bounds negative -97, main issue

for point 5, either use standard layout or set bounds accordingly.


Comments