swing - Java add/remove row to JTable? -


i trying figure out how add , remove rows jtabel. want remove rows based on first column unique id.

i creating table this:

       string[] colname = new string[] {            "id#", "country", "name", "page titel", "page url", "time"        };        object[][] products = new object[][] {            {                "867954", "usa", "todd", "start", "http://www.url.com", "00:04:13"            }, {                "522532", "usa", "bob", "start", "http://www.url.com", "00:04:29"            }, {                "4213532", "usa", "bill", "start", "http://www.url.com", "00:04:25"            }, {                "5135132", "usa", "mary", "start", "http://www.url.com", "00:06:23"            }        };          table = new jtable(products, colname); 

how add new row , delete row id # 867954 ?

you can if use defaulttablemodel:

defaulttablemodel dtm = new defaulttablemodel(products, colname); table = new jtable(dtm); 

now can add , remove rows:

dtm.removerow(0); //remove first row dtm.addrow(new object[]{...});//add row 

if want delete row based on id, can search row id , remove then:

string searchedid = "867954";//id of product remove table int row = -1;//index of row or -1 if not found  //search row based on id in first column for(int i=0;i<dtm.getrowcount();++i)     if(dtm.getvalueat(i, 0).equals(searchedid))     {         row = i;         break;     }  if(row != -1)     dtm.removerow(row);//remove row  else     ...//not found 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -