java - How can I stop losing array data through separate method calls? -


i looking make array can used multiple methods. attempted creating global array. found out, once method adds data array runs , terminates, data lost, , null again.

the array initialized here:

package book;  import book.bookio; import java.awt.borderlayout; import java.awt.*; import javax.swing.*; import java.awt.gridbaglayout; import java.awt.gridbagconstraints; import java.awt.insets; import java.awt.event.actionlistener; import java.awt.event.actionevent;  /**  *  *   */ public class userinterface implements actionlistener {     int j=0;     int i;      //containers      string hello;     jframe frame = new jframe("java program");     jpanel toppane = new jpanel(new gridbaglayout());     jpanel bottompane = new jpanel(new gridbaglayout());     //buttons     jbutton processitem = new jbutton("process item #1");     jbutton confirmitem = new jbutton("confirm item #1");     jbutton vieworder = new jbutton("view order");     jbutton finishorder = new jbutton("finish order ");     jbutton neworder = new jbutton("new order");     jbutton exit = new jbutton("exit");     //textfields     jtextfield amount = new jtextfield();     jtextfield id = new jtextfield();     jtextfield quantity = new jtextfield();     jtextfield info = new jtextfield("");     jtextfield total = new jtextfield();     //labels     jlabel num = new jlabel("enter number of items in order:");     jlabel bookid = new jlabel("enter book id item #1:");     jlabel quantityitem = new jlabel("enter quantity item #1:");     jlabel iteminfo = new jlabel("item #1:");     jlabel subtotal = new jlabel("order subtotal 0 items(s):");     public string[] order = new string[i];     int k = 0;     public void startui() {          userinterface gui = new userinterface();         gui.bookingui();      }      public void bookingui() {         //sets windows, , pane in ui          frame.setdefaultcloseoperation(jframe.exit_on_close);         gridbagconstraints c = new gridbagconstraints();         frame.setsize(800, 300);          //adding labels panel         c.insets = new insets(5, 0, 0, 0);         c.gridx = 2;         c.gridy = 1;         toppane.add(num, c);         c.gridx = 2;         c.gridy = 2;         toppane.add(bookid, c);         c.gridx = 2;         c.gridy = 3;         toppane.add(quantityitem, c);         c.gridx = 2;         c.gridy = 4;         toppane.add(iteminfo, c);         c.gridx = 2;         c.gridy = 5;         toppane.add(subtotal, c);         toppane.setbackground(color.green);         frame.add(toppane);          //add textfield panel         c.ipadx = 400;         c.insets = new insets(5, 10, 0, 0);         c.gridx = 3;         c.gridy = 1;         toppane.add(amount, c);         c.gridx = 3;         c.gridy = 2;         toppane.add(id, c);         c.gridx = 3;         c.gridy = 3;         toppane.add(quantity, c);         c.gridx = 3;         c.gridy = 4;         toppane.add(info, c);         c.gridx = 3;         c.gridy = 5;         toppane.add(total, c);          //----------------------------------------------------------buttom pane-------------------------         //adding buttons pane.---------------------------------------------------------------         gridbagconstraints b = new gridbagconstraints();         b.insets = new insets(5, 5, 5, 5);         b.ipadx = 10;         b.ipady = 10;         b.gridx = 1;         b.gridy = 0;         bottompane.add(processitem, b);         processitem.addactionlistener(this);         b.gridx = 2;         b.gridy = 0;         bottompane.add(confirmitem, b);         confirmitem.setenabled(false);         confirmitem.addactionlistener(this);         b.gridx = 3;         b.gridy = 0;         bottompane.add(vieworder, b);         vieworder.setenabled(true);         vieworder.addactionlistener(this);         b.gridx = 4;         b.gridy = 0;         bottompane.add(finishorder, b);         finishorder.setenabled(true);         finishorder.addactionlistener(this);         b.gridx = 5;         b.gridy = 0;         bottompane.add(neworder, b);         neworder.addactionlistener(this);         b.gridx = 6;         b.gridy = 0;         bottompane.add(exit, b);         exit.addactionlistener(this);         bottompane.setbackground(color.blue);         frame.add(bottompane, borderlayout.south);         frame.setsize(810, 310);         frame.setvisible(true);     }      //action listener buttons      public void actionperformed(actionevent e) {          int = integer.valueof(amount.gettext());         order = new string[i];          if (e.getsource() == processitem) {             processitem();          } else if (e.getsource() == confirmitem) {             confirmitem();             setprocessitembtn();          } else if (e.getsource() == vieworder) {             system.out.println(order.length);           } else if (e.getsource() == finishorder) {          } else if (e.getsource() == neworder) {                 neworder();         } else if (e.getsource() == exit) {             system.exit(0);         }     }  //creating getters , setters change text buttons , labels, getting text textfields.     public void setprocessitembtn() {         processitem.settext("process item #");         processitem.validate();         processitem.repaint();     }      public void setconfirmitembtn(int num) {         confirmitem.settext("confirm item #" + num);         confirmitem.validate();         confirmitem.repaint();     }      public void setvieworderbtn(string title) {          vieworder.validate();         vieworder.repaint();     }      public void setinfo(string title, string price) {          info.settext(title + " " + price);         info.validate();         info.repaint();      }      public string getamount() {         string str = amount.gettext();         return str;     }     public void confirmitem(){         = integer.valueof(amount.gettext());         int amountordered = integer.valueof(quantity.gettext());         system.out.println(k);              (k = 0; k < amountordered; k++){                order[j] = id.gettext() + " " + info.gettext();                 j++;            }            system.out.println(order[2]);           system.out.println(k);             if ((i-1)<j){                 processitem.setenabled(false);                 confirmitem.setenabled(false);              }else{                 processitem.setenabled(true);                 confirmitem.setenabled(false);             }        }     public void processitem(){          boolean iffound  = false;               confirmitem.setenabled(true);             processitem.setenabled(false);             bookio findinfo = new bookio();             iffound = findinfo.readfile(id.gettext(), this);              if (iffound == true)                 joptionpane.showmessagedialog(null, "found it");             else {                 joptionpane.showmessagedialog(null, "book id " + id.gettext() + " not found in file");                 confirmitem.setenabled(false);                 processitem.setenabled(true);             }     }     public void neworder(){              amount.settext(" ");             id.settext(" ");             quantity.settext(" ");             info.settext(" ");             total.settext(" ");        } } 

the method (confirmitem) takes in user information text fields , stores array later printed onto message box, in method,that activated separate event. method called multiple times through out programs life additional information same textfields (it same related information). when called, data last method call not present in array. null.

is there way keep data in array without losing through separate method calls? array used method hosts same issue.

thank you.

every time press button in ui, actionperformed method executes line:

order = new string[i]; 

what doing creating new, blank array of length i , setting order variable point @ new blank array, old array lost. if want expand array, must copy contents of old array new one:

string[] neworder = new string[newlength]; system.arraycopy(order, 0, neworder, 0, order.length); order = neworder; 

arrays.copyof standard, easy way in 1 line:

order = java.util.arrays.copyof(order, order.length + 1); // increase array length 1 

a list make sort of thing easier bare array, because do: orders.add(someneworder); , expand size automatically.

in case, it's not want unconditionally every time button clicked. i'm not sure how intend program behave though.


Comments