java - JComboBox: Wrong form appears after selection -


i have problems jcombobox different items select. when item selected want new, related form appear user can enter more information selected item (only 1 form per selection). in program, wrong form appears, or several forms appear in wrong order. example, if run program , select "smycke" (jewelry, parts of code in swedish), nothing happens until selected option, instance "aktie" (stock) - "smycke" form appears followed "aktie" form. how can make correct form appear?

parts of code:

public class program extends jframe {      private arraylist<valueables> allvalueables = new arraylist<>();      private jtextarea display;     private jradiobutton sortname;     private jradiobutton sortvalue;      private string[] valueables = { "smycke", "aktie", "apparat" };     private jcombobox<string> box = new jcombobox<string>(valueables);       program() {         super("sakregister");         jpanel top = new jpanel();         top.add(new jlabel("värdesaker"));         add(top, borderlayout.north);          jpanel left = new jpanel();         add(left, borderlayout.west);          jpanel right = new jpanel();         right.add(new jlabel("sortering"));         sortname = new jradiobutton("namn", true);         right.add(sortname);         sortvalue = new jradiobutton("värde");         right.add(sortvalue);         buttongroup groupb = new buttongroup();         groupb.add(sortname);         groupb.add(sortvalue);         add(right, borderlayout.east);         right.setlayout(new boxlayout(right, boxlayout.y_axis));          jpanel bottom = new jpanel();         bottom.add(new jlabel("nytt:"));          bottom.add(box);         box.addactionlistener(new boxlis());         add(bottom, borderlayout.south);          display = new jtextarea();         jscrollpane scroll = new jscrollpane(display);         display.seteditable(false);         display.setwrapstyleword(true);         add(scroll, borderlayout.center);          setsize(600, 500);         setlocationrelativeto(null);         setdefaultcloseoperation(exit_on_close);         setvisible(true);     }      class boxlis implements actionlistener {         public void actionperformed(actionevent ave) {              if (box.getselectedindex() == 0) {                 box.addactionlistener(new jewelrylis());             } else if (box.getselectedindex() == 1) {                 box.addactionlistener(new stocklis());             } else if (box.getselectedindex() == 2) {                 box.addactionlistener(new devicelis());             }         }     }      class jewelrylis implements actionlistener {         public void actionperformed(actionevent ave) {              jewelryform f = new jewelryform();             try {                 int svar = joptionpane.showconfirmdialog(program.this, f, "nytt smycke", joptionpane.ok_cancel_option);                 if (svar != joptionpane.ok_option)                     return;                  string name = f.getname();                 int stones = f.getstones();                 boolean isgold = f.getgold();                  addjewelry(name, stones, isgold);              } catch (numberformatexception e) {                 joptionpane.showmessagedialog(program.this, "fel inmatning!");             }         }     }      class stocklis implements actionlistener {         public void actionperformed(actionevent ave) {              stockform f = new stockform();             try {                 int svar = joptionpane.showconfirmdialog(program.this, f, "ny aktie", joptionpane.ok_cancel_option);                 if (svar != joptionpane.ok_option)                     return;                  string name = f.getname();                 int amount = f.getamount();                 int stockprice = f.getstockprice();                  addstock(name, amount, stockprice);              } catch (numberformatexception e) {                 joptionpane.showmessagedialog(program.this, "fel inmatning!");             }         }      }      class devicelis implements actionlistener {         public void actionperformed(actionevent ave) {              deviceform f = new deviceform();             try {                 int svar = joptionpane.showconfirmdialog(program.this, f, "ny apparat", joptionpane.ok_cancel_option);                 if (svar != joptionpane.ok_option)                     return;                  string name = f.getname();                 int purchaseprice = f.getpurchaseprice();                 int wear = f.getwear();                  adddevice(name, purchaseprice, wear);              } catch (numberformatexception e) {                 joptionpane.showmessagedialog(program.this, "fel!");             }         }      } 

in boxlis listner instead of showing appropriate form adding actionlistnere combo box.

following changes code fix issue.

box.addactionlistener(new actionlistener() {         @override         public void actionperformed(actionevent e) {             if (box.getselectedindex() == 0) {                 showjewlryform();             } else if (box.getselectedindex() == 1) {                 showstockform();             } else if (box.getselectedindex() == 2) {                 showdeviceform();             }          }     });          private void showjewlryform() {         jewelryform f = new jewelryform();         try {             int svar = joptionpane.showconfirmdialog(program.this, f, "nytt smycke", joptionpane.ok_cancel_option);             if (svar != joptionpane.ok_option)                 return;              string name = f.getname();             int stones = f.getstones();             boolean isgold = f.getgold();              addjewelry(name, stones, isgold);          } catch (numberformatexception e) {             joptionpane.showmessagedialog(program.this, "fel inmatning!");         }     }      private void showstockform() {          stockform f = new stockform();         try {             int svar = joptionpane.showconfirmdialog(program.this, f, "ny aktie", joptionpane.ok_cancel_option);             if (svar != joptionpane.ok_option)                 return;              string name = f.getname();             int amount = f.getamount();             int stockprice = f.getstockprice();              addstock(name, amount, stockprice);          } catch (numberformatexception e) {             joptionpane.showmessagedialog(program.this, "fel inmatning!");         }      }      private void showdeviceform() {          deviceform f = new deviceform();         try {             int svar = joptionpane.showconfirmdialog(program.this, f, "ny apparat", joptionpane.ok_cancel_option);             if (svar != joptionpane.ok_option)                 return;              string name = f.getname();             int purchaseprice = f.getpurchaseprice();             int wear = f.getwear();              adddevice(name, purchaseprice, wear);          } catch (numberformatexception e) {             joptionpane.showmessagedialog(program.this, "fel!");         }      } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -