java - It does not add to an arrayList for some reasons -


i've got strange thing, i'm trying add arraylist not add, values , everything. please check code , brigthen me up.

the class trying add:

public class manualproductgui extends jdialog {  private final jpanel contentpanel = new jpanel(); private jtextfield barcodefield; private jtextfield idfield; private jtextfield namefield; private jtextfield pricefield; private jtextfield quantityfield; private jtextfield infofield;  basketcontainer bc = new basketcontainer();  private static final long serialversionuid = 1l;  /**  * launch application.  */ public static void main(string[] args) {     try {         manualproductgui dialog = new manualproductgui();         dialog.setdefaultcloseoperation(jdialog.dispose_on_close);         dialog.setvisible(true);     } catch (exception e) {         e.printstacktrace();     } }  /**  * create dialog.  */ public manualproductgui() {     settitle("product search");     setbounds(100, 100, 450, 300);     getcontentpane().setlayout(new borderlayout());     contentpanel.setborder(new emptyborder(5, 5, 5, 5));     getcontentpane().add(contentpanel, borderlayout.center);     contentpanel.setlayout(null);       barcodefield = new jtextfield();     barcodefield.settext("enter barcode");     barcodefield.setbounds(10, 11, 157, 20);     contentpanel.add(barcodefield);     barcodefield.setcolumns(10);     barcodefield.addmouselistener(new mouseadapter()      {         public void mouseclicked(mouseevent e)         {             barcodefield.settext("");         }     });      barcodefield.addactionlistener(new actionlistener()     {         public void actionperformed(actionevent e)         {             int barcode = integer.parseint(barcodefield.gettext());             productctr prodctr = new productctr();              product prod = prodctr.searchproductbybarcode(barcode);             buildfields(prod);         }     });      infofield = new jtextfield();     infofield.seteditable(false);     infofield.setbounds(177, 133, 86, 20);     contentpanel.add(infofield);     infofield.setcolumns(10);     {         jpanel buttonpane = new jpanel();         buttonpane.setlayout(new flowlayout(flowlayout.right));         getcontentpane().add(buttonpane, borderlayout.south);         {             jbutton okbutton = new jbutton("add");             okbutton.setactioncommand("add");             buttonpane.add(okbutton);             okbutton.addactionlistener(new actionlistener()             {                 public void actionperformed(actionevent e)                 {                     addtobasket();                     setvisible(false);                     dispose();                 }             });             getrootpane().setdefaultbutton(okbutton);         }         {             jbutton cancelbutton = new jbutton("cancel");             cancelbutton.setactioncommand("cancel");             cancelbutton.addactionlistener(new actionlistener()             {                 public void actionperformed(actionevent e)                 {                     setvisible(false);                     dispose();                 }             });             buttonpane.add(cancelbutton);         }     } }  private void addtobasket() {     int barcode = integer.parseint(barcodefield.gettext());     productctr prodctr = new productctr();      product prod = prodctr.searchproductbybarcode(barcode);      bc.addproduct(prod); } 

}

and here part of container class:

private arraylist<product> listofitems; public basketcontainer() {     listofitems = new arraylist<>(); }  public void addproduct(product prod) {     listofitems.add(prod); } public arraylist<product> getproducts() {     return listofitems; } 

it prints out info on screen, not add. missing something?

thank you.

you create new instance of basketcontainer every time, new instances have new clear arraylist.

to fix should create basketcontainer instance somewhere (maybe in init block?) , save reference, use add elements.


Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -