java - How to add continuously data from JTextField to JTable -


i want add continuously data jtextfields jtable. when click add button, text jtextfields have inserted in jtable.

this code generates 1 row when click add button. want row added previous rows inserted.

    public void actionperformed(actionevent arg0) {         defaulttablemodel model = new defaulttablemodel();         table_1.setmodel(model);         model.addcolumn("product name");         model.addcolumn("product price");         model.addcolumn("quantity");          string name = jframe_pname.gettext().trim();         string price = jframe_pprice.gettext().trim();         string quantity = jframe_quantity.gettext().trim();         string st[] = {name, price, quantity};         model.addrow(st);     } 

do need add eventhandler table? thank you. please me assignment.

move part:

    defaulttablemodel model = new defaulttablemodel();     table_1.setmodel(model);     model.addcolumn("product name");     model.addcolumn("product price");     model.addcolumn("quantity");  

to constructor , define model instance member. don't create table model each button click. below part enough actionperformed.

public void actionperformed(actionevent arg0) {      string name = jframe_pname.gettext().trim();     string price = jframe_pprice.gettext().trim();     string quantity = jframe_quantity.gettext().trim();     string st[] = {name, price, quantity};     model.addrow(st); } 

edit:

if share full code, can tell put above parts. now, below example code can guide you.

public class tableclass {      defaulttablemodel model;       public tableclass() {         model = new defaulttablemodel();         table_1.setmodel(model);         model.addcolumn("product name");         model.addcolumn("product price");         model.addcolumn("quantity");            jbutton addbutton = jbutton("add");         addbutton.addactionlistener(new actionlistener() {              @override             public void actionperformed(actionevent e) {                  string name = jframe_pname.gettext().trim();                  string price = jframe_pprice.gettext().trim();                  string quantity = jframe_quantity.gettext().trim();                  string st[] = {name, price, quantity};                  model.addrow(st);             }         })      } } 

Comments