java - How to fill a jframe table from an arraylist? -


i have jtable created using java gui , need fill arraylist. i'm not familiar java , have no idea how fill jtable. don't know if need use tablemodel or jtable table = new jtable(data, columnnames). code:

 public void filltable(){      arraylist a=new arraylist();     jframe4 j=new jframe4();     string m=j.getloggedemail();    //  a=s.getemails(m);   //this example of arraylist      a.add("sender@gmail.com");     a.add("my subject");     a.add("hiiii");      try {         string[] columnnames = {"sender",                         "subject",                         "body",                      };           //here need fill table               }          catch(exception e) {            joptionpane.showmessagedialog(null, e);         }     } 

this declaration of jtable1 need fill:

   jtable1.setmodel(new javax.swing.table.defaulttablemodel(         new object [][] {             {null, null, null},             {null, null, null},             {null, null, null},             {null, null, null}         },         new string [] {             "sender", "subject", "body"         }     ));     jscrollpane1.setviewportview(jtable1); 

i don't know if need use tablemodel or jtable table = new jtable(data, columnnames)

depends on needs, can either. demonstrate how use latter. if want use own tablemodel, see link in @andrew thompson's comment.

public class example extends jframe {      example() {          string[] colnames = new string[] {"sender", "subject", "body"};          list<string> list = new arraylist<>();         list.add("sender@gmail.com");         list.add("my subject");         list.add("hiiii");         list.add("sender2@gmail.com");         list.add("my subject2");         list.add("hiiii2");          string[][] rowdata = new string[list.size() / colnames.length][colnames.length];         (int = 0; < rowdata.length; i++) {             (int j = 0; j < rowdata[i].length; j++) {                 rowdata[i][j] = list.get(i * colnames.length + j);             }         }          jtable table = new jtable(rowdata, colnames);          add(new jscrollpane(table));         setdefaultcloseoperation(exit_on_close);         pack();         setlocationrelativeto(null);         setvisible(true);     }      public static void main(string[] args) {          new example();     } } 

just note if list not contain 3 elements in row, whole row not displayed.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -